Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: LordVader on May 28, 2005, 01:09 AM

Title: What is Best way to get BYTE values from WORDS? or other values(c++)
Post by: LordVader on May 28, 2005, 01:09 AM
Say i had a DWORD..

(DWORD)szDWord = 1a2b;       

szDWord contains BYTE's 1a & 2b

Can someone give me a small example of how I could go about getting the individual bytes from that dword value?

I'm assuming a for or while loop but unsure not had luck with it yet.
Title: Re: What is Best way to get BYTE values from WORDS? or other values(c++)
Post by: K on May 28, 2005, 03:18 AM
I'm not sure why you'd prefix a DWORD with "sz", but other than that...


typedef unsigned char uint8_t;
DWORD dwFoo = 0xABCD0123;
uint8_t* bytes = (uint8_t*)&dwFoo;

for(int i = 0; i < 3; ++i)
{
   std::cout << "bytes[" << i << "] = " << (int)bytes[i] << std::endl;
}
Title: Re: What is Best way to get BYTE values from WORDS? or other values(c++)
Post by: UserLoser. on May 28, 2005, 12:39 PM

#define LOBYTE(w)           ((BYTE)((DWORD_PTR)(w) & 0xff))
#define HIBYTE(w)           ((BYTE)((DWORD_PTR)(w) >> 8))

WORD TestValue = 0xAB12;
printf("First 8 bits: %02x\r\n", HIBYTE(TestValue));
printf("Last 8 bits: %02x\r\n", LOBYTE(TestValue));


Title: Re: What is Best way to get BYTE values from WORDS? or other values(c++)
Post by: LordVader on May 29, 2005, 03:08 AM
ty guys, and the sz is just a short tag i use on variables, habit..

I'll try those methods out should work for what im doing.
I'd looked at using <<|>> right|left shifts but wasn't sure how to empliment, that will be very handy, Ty Both.
Title: Re: What is Best way to get BYTE values from WORDS? or other values(c++)
Post by: K on May 29, 2005, 03:28 AM
Quote from: LordVader on May 29, 2005, 03:08 AM
ty guys, and the sz is just a short tag i use on variables, habit..

I'll try those methods out should work for what im doing.
I'd looked at using <<|>> right|left shifts but wasn't sure how to empliment, that will be very handy, Ty Both.

"sz" is hungarian notation for "string, zero terminated," which is why I mentioned it.     Usually "sz" is used only as a prefix for a char* variable which is terminated by a null character.  The prefix for a double word type variable is usually "dw" -- ie, "dwFlags" pr "dwCharSet".
Title: Obligatory notational note:
Post by: Kp on May 29, 2005, 12:05 PM
Just so readers are aware: a DWORD is not a double word on modern hardware.  It is a single word (and WORD is actually a half word) -- both on ia32, of course.  On an IA64, DWORD is actually a half word and WORD is a quarter word.
Title: Re: What is Best way to get BYTE values from WORDS? or other values(c++)
Post by: shout on June 01, 2005, 11:16 AM

DWORD dwNum = 0xAABBCCDD;
BYTE *bytes = &dwNum;


bytes[0] = 0xDD
bytes[1] = 0xCC
bytes[2] = 0xBB
bytes[3] = 0xAA

Now what if, for some unknown random reason, wanted the last byte values first simply:


DWORD dwNum = 0xAABBCCDD;
BYTE bytes[4];

for(int i=0, j=4; i<4; i++, j--)
       bytes[i] = (BYTE *)dwNum[j];


Of course, this is for little-edian systems.