In one of my tasks I need to store DWORD into BYTE(s) and then convert them back. Platform is windows only.
I have found the following on this website:
//Convert an array of four bytes into a 32-bit integer.
DWORD getDwordFromBytes(BYTE* b)
{
return (b[0]) | (b[1] << 8) | (b[2] << 16) | (b[3] << 24);
};
This works for converting 4 bytes into 1 DWORD, however how would you do this for DWORD64 ?
I have tried the following but it's not returning correctly as I am losing data:
DWORD64 getDwordFromBytes64(BYTE* b)
{
return ((DWORD64)b[0]) | ((DWORD64)b[1] << 8) | ((DWORD64)b[2] << 16) | ((DWORD64)b[3] << 24);
};
Suppose I have a byte array:
BYTE[] = {0x38,0xf0,0x07,0x40,0x01,0x00,0x00,0x00}; //000000014007F038
I need to get 000000014007F038 (DWORD64) back from it correctly.
If someone would give me a solution I would very much appreciate it.
Update:
- I can accept any solution wheter C or C++.
- "Endianness" can be ignored.