convert a DWORD to BYTE[4]
-
Hello gurus, I'd like to know how I can convert a DWORD (32 bits) value into a byte array of four bytes (32 bits)?
DWORD dwLength=(DWORD) file.GetLength(); BYTE length[4];
length
contains the valuedwLength
as a 4 bytes array. Thanks :) Best regards. There is no spoon. -
Hello gurus, I'd like to know how I can convert a DWORD (32 bits) value into a byte array of four bytes (32 bits)?
DWORD dwLength=(DWORD) file.GetLength(); BYTE length[4];
length
contains the valuedwLength
as a 4 bytes array. Thanks :) Best regards. There is no spoon.for (int i = 3; i >= 0; --i)
{
length[3 - i] = (dwLength & (0xff << (i * 8))) >> (i * 8);
}
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!
Honoured as one of The Most Helpful Members of 2004
-
Hello gurus, I'd like to know how I can convert a DWORD (32 bits) value into a byte array of four bytes (32 bits)?
DWORD dwLength=(DWORD) file.GetLength(); BYTE length[4];
length
contains the valuedwLength
as a 4 bytes array. Thanks :) Best regards. There is no spoon.DWORD dwLength=(DWORD) file.GetLength();
BYTE length[sizeof(DWORD)];*(DWORD*) &length[0] = dwLength;
Keep in mind that any code doing anything with the bytes will have to account for the endian type of the CPU. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Strange things are afoot at the U+004B U+20DD
-
DWORD dwLength=(DWORD) file.GetLength();
BYTE length[sizeof(DWORD)];*(DWORD*) &length[0] = dwLength;
Keep in mind that any code doing anything with the bytes will have to account for the endian type of the CPU. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Strange things are afoot at the U+004B U+20DD
-
Hi, Thanks :) Actually I've found the following way:
length[0]=HIBYTE(HIWORD(dwLength)); length[1]=LOBYTE(HIWORD(dwLength)); length[2]=HIBYTE(LOWORD(dwLength)); length[3]=LOBYTE(LOWORD(dwLength));
which also works :) There is no spoon. -
Hi, Thanks :) Actually I've found the following way:
length[0]=HIBYTE(HIWORD(dwLength)); length[1]=LOBYTE(HIWORD(dwLength)); length[2]=HIBYTE(LOWORD(dwLength)); length[3]=LOBYTE(LOWORD(dwLength));
which also works :) There is no spoon.It works, but not the same way as my code in my earlier post. Yours forces big-endian order. Which isn't wrong per se, it's just something to be aware of. Don't try to write that byte array out to a file and read it back in as a
DWORD
, for example. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Strange things are afoot at the U+004B U+20DD -
Hello gurus, I'd like to know how I can convert a DWORD (32 bits) value into a byte array of four bytes (32 bits)?
DWORD dwLength=(DWORD) file.GetLength(); BYTE length[4];
length
contains the valuedwLength
as a 4 bytes array. Thanks :) Best regards. There is no spoon. -
Hello gurus, I'd like to know how I can convert a DWORD (32 bits) value into a byte array of four bytes (32 bits)?
DWORD dwLength=(DWORD) file.GetLength(); BYTE length[4];
length
contains the valuedwLength
as a 4 bytes array. Thanks :) Best regards. There is no spoon.I have one more method, but not good. union { DWORD dwNumber; BYTE byArry[4]; } http://www.priyank.in/
-
I have one more method, but not good. union { DWORD dwNumber; BYTE byArry[4]; } http://www.priyank.in/
Priyank Bolia wrote: ...but not good Why? This is the method I've used in years past. I find it easier to read than a bunch of pointers ands casts.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
Priyank Bolia wrote: ...but not good Why? This is the method I've used in years past. I find it easier to read than a bunch of pointers ands casts.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
I just write that thinking that people like it or not. I still use it in imaging programs, where image documents are large size and memory and speed are a constraint. It saves memory and helps out small programmers from the pointers nightmare, and the main point is it will save precious time as compare to for loops and memcpy operations etc. http://www.priyank.in/