Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

billybob

dword to byte and byte to dword functions, why?

This topic is 5586 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i don''t get why there are functions like this, but then again this might be flawed. as i understand it, the subscript operator does size of type * index + pointer address. correct? now lets say you cast a 2 DWORD array to a BYTE.
DWORD Array[2] = { ..., ...};
BYTE * ArrayInBytes = (BYTE*)&Array[0];
 
now, since the subscript operator adds index * size of type, wouldn''t the following be true? address of Array[1] = address of ArrayInBytes[4] if you break that down, wouldn''t it be Array[0] + sizeof(DWORD) * 1, and ArrayInBytes[0] + sizeof(BYTE) * 4, which is true? so, why doesn''t a simple cast from a dword to a byte * convert a dword to 4 bytes?
dword something = 00001111 11110000 10101010 01010101
byte * bytes = &something;

bytes[0] = 00001111   //wouldn''t this be true?
bytes[1] = 11110000
bytes[2] = 10101010
bytes[3] = 01010101
 

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
quote:

dword something = 00001111 11110000 10101010 01010101
byte * bytes = &something;
bytes[0] = 00001111 //wouldn''t this be true?
bytes[1] = 11110000
bytes[2] = 10101010
bytes[3] = 01010101



that depends on what machine you are trying it on.
if you tried it on a motorola based processor, it would be as you described it. But if you try it on an x86 based processor the order would be reversed.

Google around for MSB and LSB order...

Share this post


Link to post
Share on other sites
would it reverse the order of the bytes only, or the entire dword, ie:

bytes[0] = 01010101
bytes[1] = 10101010
bytes[2] = 11110000
bytes[3] = 00001111

or

bytes[0] = 10101010
bytes[1] = 01010101
bytes[2] = 00001111
bytes[3] = 11110000

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
like this:

bytes[0] = 01010101
bytes[1] = 10101010
bytes[2] = 11110000
bytes[3] = 00001111

(the order of the bytes are reversed)

Share this post


Link to post
Share on other sites
and portability -- the program (with the proper library for that specific processor)can change the DWORD to BYTE regardless of what it is ported to

Share this post


Link to post
Share on other sites
A x86 based processor access the bytes in LITTLE_ENDIAN while a motorola based
processor access the bytes in BIG_ENDIAN


KaMiKaZe

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!