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