Re: Hexadecimal values



On 2009-11-16, lancer6238@xxxxxxxxx <lancer6238@xxxxxxxxx> wrote:
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.

No, you don't.

You are becoming confused and thinking that you care about representation.
You don't.

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.

Okay.

So far, I'm using

char clength[3];
sprintf(clength, "%x%x", file_buffer[2], file_buffer[3]);
length = strtol(clength, NULL, 16);

This is wrong in several ways.

And I am able to get the correct results length = 39.

Lucky!

Here is the thing. First off, consider what happens if the two
values are 0x02 and 0x03. You will get "23", so you'll treat
0x0203 as if it were 0x23. You want %02x%02x.

Secondly, "%x" may often produce at least two characters, and
you need another byte for the trailing null. Assuming that you
never see values outside the range 0..255, you still need at
least 5 bytes.

Finally, WHY WHY WHY WHY are you carefully converting the
integer values you want into a string, then converting them back?

Try:
length = (file_buffer[2] * 256) + file_buffer[3];

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.

Right.

Because you have populated clength with the STRING "35".

So you're stashing the CHARACTER '3' in your array. And since you
appear to be using ASCII, it happens that '3' is the same value
as '\x33'.

But
array[2] = clength[1];
array[6] = clength[1];
gets me the correct 0x35 value.

Lucky!

Why is that?

Because '5' == '\x35' in your environment.

In short, pure coincidence. If your value had come out 0x40, then
you would be seeing 0x34 and 0x30 for the two values, and you'd
have a better guess as to what's wrong.

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]);

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"?

Because you're on a machine where characters are signed, and when
a signed -1 is promoted to int, it stays -1, and on your machine int
is 32-bits, and -1 is 0xffffffff.

Summary:

1. If you wanna work with raw bit values, use unsigned char, don't
rely on plain char.
2. Don't convert to and from hex when what you have is raw data.

Print stuff in hex or decimal or whatever you want for human readers,
but if you have a value stored in two adjacent bytes, you do not
need to "convert" it.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet-nospam@xxxxxxxxx
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
.



Relevant Pages

  • Re: [9fans] Re: Building GCC
    ... We could store the raw data in binary files and have C programs access the data with a standard interface. ... void amamkechar(Atom *a, char c) ... void drawatom(Image *d, Atom *a, Point loc, Image *textcolor, Point textcolorpt, char *font) ... We could add an ability to read a string of characters up to EOF or an image to make it simpler. ...
    (comp.os.plan9)
  • Re: Converting Packed String to Zoned Decimal
    ... It does not unpack properly using RPG simple "MOVE" op code. ... Here is a HEX value of Packed String ... The first output is the input broken out to its hex characters and the second output shows those hex characters returned to the original byte format. ... Then converts that hex back to char for output. ...
    (comp.sys.ibm.as400.misc)
  • Re: Hexadecimal values
    ... This will populate the first three characters of "foo" with: ... and the raw data. ... When we dump the contents of memory, we tend to use hex ...
    (comp.lang.c)
  • Re: convert byte to char
    ... when converting to char I expect to get ... the hex value of FF. ... but after converting I get 3F. ... characters are converted correctly. ...
    (comp.lang.java.programmer)
  • convert byte to char
    ... when converting to char I expect to get ... the hex value of FF. ... but after converting I get 3F. ... characters are converted correctly. ...
    (comp.lang.java.programmer)