Hi,
suppose I have the following buffer:
This buffer represents the number "123456".Code:BYTE b[4];
b[0] = 0x40;
b[1] = 0xe2;
b[2] = 0x01;
b[3] = 0x00;
How do I convert this buffer into a ULONG ??
Printable View
Hi,
suppose I have the following buffer:
This buffer represents the number "123456".Code:BYTE b[4];
b[0] = 0x40;
b[1] = 0xe2;
b[2] = 0x01;
b[3] = 0x00;
How do I convert this buffer into a ULONG ??
Good question, is ulong associated with an Unsigned 64-bit integer? I am new to programming in a sense. At least, every time I turn around, I feel like im noob.
Don't know what exactly the correct way is, but you can always shift them into the ulong.
Code:ULONG b;
b= 0x40;
b+= (0xe2 << 8);
b+= (0x01 << 16);
b+= (0x00 << 24);
Why would you do that? and also, was I correct in the statement I made earlier?
Thanks - It works fine !
Although some will recommend against it, you could do a simple cast:Hope that helps.Code:BYTE b[4];
ULONG lVal = 0;
b[0] = 0x40;
b[1] = 0xe2;
b[2] = 0x01;
b[3] = 0x00;
lVal = *(ULONG*)&b[0];
krmed, why do ppl argue against your proposed method ? After all, wouldn't that work on both big and little endian architecture *** opposed to just shifting them up which only works on BIG endian ?
Nope, exactly the other way around. Shifting works on both big and little endian and casting works only by accident.Quote:
After all, wouldn't that work on both big and little endian architecture *** opposed to just shifting them up which only works on BIG endian ?
The casting as noted works on little endian systems, and will fail with big endian. Simple casting as I showed can be unsafe (that's why some will say don't do it), and you must therefore understand when a simple cast is acceptable in order to prevent big time problems.
However, it is not "by accident" that it works on little endian systems...it works because the layout of the data is correct for simple casting on little endian systems.
a simple union would also do ;)
union ul
{
BYTE b[4];
ULONG result;
} ;
setting the correct bytes in b in the correct order ( according to the endianess of the machine ofc)
you can the access result as an ulong.