L
lancer6238
Hi all,
I have a file that I'm trying to read into memory. I want to store the
hexadecimal values of the file content into a buffer.
Say the hexadecimal value is 12 00 00 27 09 10, I want to read the 3rd
and 4th bytes as the length, i.e. 0x0027 = 39 in decimal and assign
length = 39.
So far, I'm using
char clength[3];
sprintf(clength, "%x%x", file_buffer[2], file_buffer[3]);
length = strtol(clength, NULL, 16);
And I am able to get the correct results length = 39.
Then I want to add 14 to the length, and put the hexadecimal value of
the result into a character array (storing hexadecimal values).
This character array is initialized to
array[] = {0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00,
0x00};
I want the hexadecimal value of (length+14 = 53 = 0x35) to replace the
2 0xff in array[], so I tried
sprintf(clength, "%x", length+14);
array[2] = clength[0];
array[6] = clength[0];
But I got 0x33 instead of 0x35.
But
array[2] = clength[1];
array[6] = clength[1];
gets me the correct 0x35 value.
Why is that?
Also, when I printed out the contents of the array[] before any
modifications were done, using
for (i = 0 ; i < 10 ; i++)
printf("%x ", array);
I get "0 0 ffffffff 0 0 0 ffffffff 0 0 0". Why didn't I get "0 0 ff 0
0 0 ff 0 0 0"?
Thank you.
Regards,
Rayne
I have a file that I'm trying to read into memory. I want to store the
hexadecimal values of the file content into a buffer.
Say the hexadecimal value is 12 00 00 27 09 10, I want to read the 3rd
and 4th bytes as the length, i.e. 0x0027 = 39 in decimal and assign
length = 39.
So far, I'm using
char clength[3];
sprintf(clength, "%x%x", file_buffer[2], file_buffer[3]);
length = strtol(clength, NULL, 16);
And I am able to get the correct results length = 39.
Then I want to add 14 to the length, and put the hexadecimal value of
the result into a character array (storing hexadecimal values).
This character array is initialized to
array[] = {0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00,
0x00};
I want the hexadecimal value of (length+14 = 53 = 0x35) to replace the
2 0xff in array[], so I tried
sprintf(clength, "%x", length+14);
array[2] = clength[0];
array[6] = clength[0];
But I got 0x33 instead of 0x35.
But
array[2] = clength[1];
array[6] = clength[1];
gets me the correct 0x35 value.
Why is that?
Also, when I printed out the contents of the array[] before any
modifications were done, using
for (i = 0 ; i < 10 ; i++)
printf("%x ", array);
I get "0 0 ffffffff 0 0 0 ffffffff 0 0 0". Why didn't I get "0 0 ff 0
0 0 ff 0 0 0"?
Thank you.
Regards,
Rayne