Endian to Hex

Started by
9 comments, last by Krakken 20 years, 5 months ago
How would I convert data stored in little endian format to hex format so that I can work with it? Is this something that is usually done because I can''t see an easy way of working with the little endian format. Thanks, Nat.
Advertisement
What kind of "working" do you mean? In what format do you need the data? If you''re talking about integers, the numbers aren''t "stored" in "little endian format" or "hex format" it''s stored in binary numbers. But you don''t have to worry about how it''s stored, it''s the values you use.
2 + 2 = 5 for extremely large values of 2
Hey Krakken,

It''s pretty simple actually. Endian-ness refers to how the -bytes- of the value are ordered, and with 8 bit bytes, 1 byte corresponds to 2 digits of hex.

So the literal value of 0x12345678

looks like this in memory (little endian):

78 56 34 12

and like this in big-endian:

12 34 56 78

But the thing is, you almost never have to worry about this difference, because it all happens behind the scenes. You usually don''t have to worry about how the values look in memory, because all the math, all the everything will work exactly the same with either endianness.

The only time you have to worry about endianness is if you are transferring data between a program compiled as big-endian and a program compiled as little-endian (whether through save files or network communication or something else).
Unless you decide to use pointer tricks to convert the data, then endianness won''t make a difference. Of course, I don''t have a clue about what you mean by "working" with the data. If you just mean displaying it in hexidecimal, then most languages have some standard library routine that will do such a thing. If you''re just talking about manipulating the data, then—like Goldfish said—there''s no need to worry about the internal representation of the values.
Hi,

Sorry I haven''t made it 100% clear what I mean exactly. I have a set of WORD and DWORD values (2 and 4 bytes). I need to convert the endian values I have stored in those types to something I can understand easily.

For example, one thing I need to do is read a bitmap and get the width and height of the image. Now I know the width is "240" and the height is "128" but the values that I read are "15728640" for width and "8388608" for height.

To me those numbers make no sense at all so i''m looking for a way to either read those easily or to convert them into hex/integers so I can read them easily or maybe even an alternate method I haven''t thought of...

Thanks, Nat.
Are you working on a little endian system?. I believe the BMP standard is that all values are big-endian. This is why it''s best to just use a 3rd-party file loader for these things. I recommend FreeImage.

Anyway, 15728640 = 0x00F00000 in hex, and 8388608 = 0x00800000 (I used the windows calculator to find that out)

Then to convert endianness, you break up the numbers into 2-digit groups, and flip them.

0x00 f0 00 00 flipped is 0x00 00 f0 00
0x00 80 00 00 flipped is 0x00 00 80 00

and those are 61440 and 32768.

BUT, 0xf0 is 240 and 0x80 is 128. So my guess is that your problem is not endianness, just that you are reading from the wrong place in the file. OR MAYBE, the heights and widths are suppossed to be words? (2 bytes). That would certainly solve the problem
Just to get this straight: endianness, little endian, or whatever, is not an alternative to hex, binary or decimal. They are completely different concepts: little and big endian refer to how the bytes are stored, and hex, binary and decimal refer to various ways of writing out the numbers. They are always stored the same way.
Ah i see! I was getting confused when it is a simple answer. I thought endian was an encoded format.

Thanks very much, Nat.
Yey now everything makes a lot of sense. I needed to use WORDS and spacer WORDS to make it function correctly. Thank you everyone!
Hmm, that sounds weird that you need spacer values. In fact, checking the reference it looks like height and width are LONGs (4 bytes) in bitmaps. But I guess if it works, then you can always leave it as it is.

This topic is closed to new replies.

Advertisement