• ### What is your GameDev Story?

Public Group

#### Archived

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

# dword to byte and byte to dword functions, why?

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

## 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 on other sites
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 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 on other sites
like this:

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

(the order of the bytes are reversed)

##### Share on other sites
It only reverses the order of the bytes on x86 processors.

##### Share on other sites
alright thanks, i guess they make dword to byte functions and vice versa just for readability

##### 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 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

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• ### Forum Statistics

• Total Topics
634051
• Total Posts
3015244
×