|
-
April 25th, 2009, 04:39 PM
#1
Converting BYTE[] to ULING
Hi,
suppose I have the following buffer:
Code:
BYTE b[4];
b[0] = 0x40;
b[1] = 0xe2;
b[2] = 0x01;
b[3] = 0x00;
This buffer represents the number "123456".
How do I convert this buffer into a ULONG ??
-
April 25th, 2009, 04:43 PM
#2
Re: Converting BYTE[] to ULING
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.
-
April 25th, 2009, 05:05 PM
#3
Re: Converting BYTE[] to ULING
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);
-
April 25th, 2009, 05:06 PM
#4
Re: Converting BYTE[] to ULING
Why would you do that? and also, was I correct in the statement I made earlier?
-
April 25th, 2009, 05:09 PM
#5
Re: Converting BYTE[] to ULING
-
April 25th, 2009, 07:16 PM
#6
Re: Converting BYTE[] to ULING
Although some will recommend against it, you could do a simple cast:
Code:
BYTE b[4];
ULONG lVal = 0;
b[0] = 0x40;
b[1] = 0xe2;
b[2] = 0x01;
b[3] = 0x00;
lVal = *(ULONG*)&b[0];
Hope that helps.
Be sure to rate those who help!
-------------------------------------------------------------
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
April 26th, 2009, 04:48 PM
#7
Re: Converting BYTE[] to ULING
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 ?
-
April 27th, 2009, 02:22 AM
#8
Re: Converting BYTE[] to ULING
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.
-
April 27th, 2009, 05:47 AM
#9
Re: Converting BYTE[] to ULING
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.
Be sure to rate those who help!
-------------------------------------------------------------
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
April 27th, 2009, 06:25 AM
#10
Re: Converting BYTE[] to ULING
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|