|
|
 |
 |
 |
 |
HEXADECIMAL to STRING
Hi, suppose that I have a string that is an hexadecimal number, in order to print this string I have to do: void print_hex(unsigned char *bs, unsigned int n){ int i; for (i=0;i<n;i++){ printf("%02x",bs[i]); } }
where bs is an hexadecimal string, now i want to create a function that given bs return a printable string containing the hexadecimal value of bs, i tried doing this: int hex2str(char* digest, char* result,int len){ int i; char *app=malloc(sizeof(char)); result=malloc(strlen(digest)); for (i=0;i<len;i++){ sprintf(app,"%02x",digest[i]); strcat(result,app); } }
When i print result it seems to give me some polluted string with some unprintable character, any suggestion? Thanks in advance, Andrea
On Jun 3, 4:37 pm, Andrea <aciru@gmail.com> wrote:
> Hi, > suppose that I have a string that is an hexadecimal number, in order > to print this string I have to do: > void print_hex(unsigned char *bs, unsigned int n){ > int i; > for (i=0;i<n;i++){ > printf("%02x",bs[i]); > } > } > where bs is an hexadecimal string, now i want to create a function > that given bs return a printable string containing the hexadecimal > value of bs, i tried doing this: > int hex2str(char* digest, char* result,int len){ > int i; > char *app=malloc(sizeof(char)); > result=malloc(strlen(digest)); > for (i=0;i<len;i++){ > sprintf(app,"%02x",digest[i]); > strcat(result,app); > } > } > When i print result it seems to give me some polluted string with some > unprintable character, > any suggestion? > Thanks in advance, > Andrea
I corrected the function now seems to work this is the code: char* hex2str(unsigned char *hex,unsigned int len){ int i; char *app; char *buf; app=malloc(sizeof(char)); buf=malloc(strlen(hex)); strcpy(buf,""); for (i=0;i<len;i++){ sprintf(app,"%02x",hex[i]); strcat(buf,app); } return buf; }
it returns a printable string representing hexadecimal value of hex, anu observation? thx
"Andrea" writes: > suppose that I have a string that is an hexadecimal number, in order > to print this string I have to do: > void print_hex(unsigned char *bs, unsigned int n){ > int i; > for (i=0;i<n;i++){ > printf("%02x",bs[i]); > } > } > where bs is an hexadecimal string,
So bs is a collection of unsigned char that contain the binary values 0..15 only? How can you do that? A string is terminated by a binary zero. How can you distinguish *that* binary zero from other binary zeros legitimately part of the number? <stopped reading here> now i want to create a function
> that given bs return a printable string containing the hexadecimal > value of bs, i tried doing this: > int hex2str(char* digest, char* result,int len){ > int i; > char *app=malloc(sizeof(char)); > result=malloc(strlen(digest)); > for (i=0;i<len;i++){ > sprintf(app,"%02x",digest[i]); > strcat(result,app); > } > } > When i print result it seems to give me some polluted string with some > unprintable character, > any suggestion?
Andrea <aciru @gmail.com> writes: > Hi, > suppose that I have a string that is an hexadecimal number, in order > to print this string I have to do: > void print_hex(unsigned char *bs, unsigned int n){ > int i; > for (i=0;i<n;i++){ > printf("%02x",bs[i]); > } > } > where bs is an hexadecimal string, now i want to create a function > that given bs return a printable string containing the hexadecimal > value of bs, i tried doing this: > int hex2str(char* digest, char* result,int len){ > int i; > char *app=malloc(sizeof(char)); > result=malloc(strlen(digest)); > for (i=0;i<len;i++){ > sprintf(app,"%02x",digest[i]); > strcat(result,app); > } > }
You don't need to format each digit and the glue them together, but you must get all your sizes right first. You have "len" digits. When tuned into a string, each one needs two characters to represent it and you need space for a terminating null: char *result = malloc(<you work it out>); Note: sizeof char is 1 *by definition* so it is usually left out of such calculations. You can put the digits in the right place in the result using pointer arithmetic: for (i = 0; i < len; i++) sprintf(<your code here>, "%02x", digest[i]); Also, you need to understand how to get values out of functions in C. In your example code, hex2str returns an int, but you don't return anything. What did you intend? The simplest solution is to return the string you have just built. Finally, (at least from me) you *must* test the return from malloc and deal appropriately with an allocation failure. I have not put this all together because it looks too much like homework (or coursework as we tend to call it here in the UK). -- Ben.
On Sun, 03 Jun 2007 15:06:08 -0000, Andrea <aciru @gmail.com> wrote: >On Jun 3, 4:37 pm, Andrea <aciru @gmail.com> wrote: >> Hi, >> suppose that I have a string that is an hexadecimal number, in order >> to print this string I have to do: >> void print_hex(unsigned char *bs, unsigned int n){ >> int i; >> for (i=0;i<n;i++){ >> printf("%02x",bs[i]); >> } >> }
Based on this code, I assume that you do not have a string as C uses the term but an array of bytes. Instead of unsigned char x[] = "1001"; you have unsigned char x[] = {0x10,0x01}; because for the former your code would produce 31303031 on an ASCII system and I think you want 1001 which your code would produce for the latter. >> where bs is an hexadecimal string, now i want to create a function >> that given bs return a printable string containing the hexadecimal >> value of bs, i tried doing this:
snip obsolete code that should never have been quoted >I corrected the function now seems to work this is the code: >char* hex2str(unsigned char *hex,unsigned int len){ >int i; >char *app; >char *buf; >app=malloc(sizeof(char));
sizeof char is always one. >buf=malloc(strlen(hex));
You cannot use strlen on your "hexadecimal string" because it is perfectly reasonable to have a byte of 0x00 in the middle. For example, 65537 is 0x010001 which in your hex string would look like 0x01, 0x00, 0x01. That second byte would cause strlen to return a value of 1 when you want 3. You seem to have partially recognized this since you pass len into your function. I don't understand either of these calls to malloc or why there are two. You only have one result and its length is guaranteed to be 2*len and its size must be at least 2*len+1 to hold the terminal '\0'. You should always check that malloc succeeded before dereferencing its return value. >strcpy(buf,"");
*buf = '\0'; is much simpler. >for (i=0;i<len;i++){ > sprintf(app,"%02x",hex[i]);
This will always produce three bytes of output. Since app points to only one byte, you have overflowed the allocated memory and invoked undefined behavior. Why not define app as char app[3]; and make life easy on yourself? > strcat(buf,app);
buf points to an area of memory large enough to hold the results of only the first few (up to half) iterations. At the latest, once you get past half the iterations, you overflow the allocated space and invoke undefined behavior. > } >return buf; >}
You never freed app. This causes a memory leak. >it returns a printable string representing hexadecimal value of hex, >anu observation?
Yes, you were extremely unlucky. All your undefined behavior manifested itself in a manner which confused you into thinking your code was correct. If you were lucky, or had a well designed (tm) system, your program would have been terminated at the first instance and you would have known immediately that you had problems. Where do you print the result? You invoke undefined behavior again by overflowing your allocated memory in buf. Remove del for email
Andrea wrote: > Hi, > suppose that I have a string that is an hexadecimal number, in order > to print this string I have to do: > void print_hex(unsigned char *bs, unsigned int n){ > int i; > for (i=0;i<n;i++){ > printf("%02x",bs[i]); > } > } > where bs is an hexadecimal string,
First, given that print_hex does what you want, bs is a binary array, not a hexadecimal string. Hexadecimal refers to the representation that the binary value is converted to with the %02x printf specifier, not the input. > now i want to create a function > that given bs return a printable string containing the hexadecimal > value of bs, i tried doing this: > int hex2str(char* digest, char* result,int len){
I start all my functions with a header that specifies the operation, and parameter comments that explain the range and representation of the parameters. I recommend that practice as an aide to avoiding bugs from misunderstanding what the function does, what it returns, and what the requirements of the parameters are. The name of the function is misleading, since the input is not a hexadecimal array, but a binary array. A binary array is being converted to a hexadecimal string, so it would be more accurate to call it BinToHex() or BinArrayToHex(). The first parameter is not being used as a digest within the function, so should not have that name. In general, you should define the function for general use. The function doesn't care what use the binary array is to the external code. I suggest const unsigned char *binArray, /* array to be converted to ** hexadecimal */ The array elements are declared const since we are not modifying them. What is the definition of the result parameter? Without any comment to guide me, I assume that it is a pointer to an existing array to be used for the output. Reading your code, however, suggests that you are dynamically allocating the array within the function. This is a significant discrepancy. This type of error is encouraged by not defining the use of the parameter when you declare the function. If you want to allocate the return array in the function, you must return a pointer. Your declaration does not do that. You could either return the pointer as the function return value, or pass a pointer to a pointer to contain it. Since there should be no conversion errors, except for failure to allocate memory (which you can naturally indicate by returning a null pointer), I suggest returning the pointer as the function return value. It is reasonable for len to be the number of bytes in binArray to be converted. Here is my suggestion for the function declaration: /* Function: BinArrayToHex ** Description: This function converts an array of unsigned char to the ** corresponding hexadecimal string. There is no leading ** zero suppression or byte separation. The least ** significant 8 bits of each byte are converted to a ** two-digit hexadecimal value, using the characters ** 0-9,a-f. ** The resulting null-terminated string has length 2*len ** characters. It is dynamically allocated by this ** function and must be freed by the caller. ** A failure to allocate the string results in a null ** pointer return value. ** Example: binArray = {0x00, 0x1a, 0x2f}, len = 3 ** BinArrayToHex returns string: "001a2f" */ char * /* hexadecimal string of binArray or NULL */ BinArrayToHex ( const unsigned char *binArray, /* binary array to be converted */ size_t len /* # bytes to be converted */ ); I suggest writing the described function or modifying it suit your own purpose, but make the differences explicit. -- Thad
Andrea <aciru @gmail.com> writes: > suppose that I have a string that is an hexadecimal number, in order > to print this string I have to do: > void print_hex(unsigned char *bs, unsigned int n){ > int i; > for (i=0;i<n;i++){ > printf("%02x",bs[i]); > } > } > where bs is an hexadecimal string,
[snip] bs is not a hexadecimal string. First of all, bs is a pointer; pointers are used to manipulate strings, but pointers and strings, are two *very* different things.q So bs points to the first element of an array of unsigned char. That array, judging by the way you're treating it, is not hexadecimal. It's an array of numeric values; assuming 8-bit characters, each element has a value in the range 0..255. A hexadecimal string would be a character string with values '0'..'9','a'..'f' (or 'A'..'F'). The printf function with the "%x" format is used to *convert* a numeric value to its hexadecimal representation. If you had a hexadecimal string to begin with, you wouldn't need to convert it to hexadecimal. Also, be careful with the word "string", which has a very specific meaning in C. A string is "a contiguous sequence of characters terminated by and including the first null character" (C99 7.1.1p1). Not all character arrays are strings. -- Keith Thompson (The_Other_Keith) k@mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"
|
 |
 |
 |
 |
|