JJR
Joined: 22 Feb 2004 Posts: 1104
|
Posted: Thu Jan 27, 2005 12:22 am Post subject: |
|
|
Well, I suppose you could do it much like C and use a union structure.
Of course the down and dirty method always works:
Code: | import std.stdio;
void main()
{
byte[4] b32;
b32[0]=2; // Low byte for little Endian, high for Big Endian
b32[1]=4;
b32[2]=8;
b32[3]=6; // High byte for little Endian, low for Big Endian
uint* p32;
uint i32;
// map the uint pointer to the byte array
p32 = cast(uint*)&b32[0];
// i32 can be assigned the value of the pointer
i32 = *p32;
// The following could use *p32 or i32 to display the values
// Display assuming little Endian (x86)
printf("\n*p32 = ?lu \n", i32);
printf("\nBYTE(4) of i32 = ?lu (HI)\n", i32 >> 24);
printf("\nBYTE(3) of i32 = ?lu \n", (i32 & 0x00ffffff) >> 16);
printf("\nBYTE(2) of i32 = ?lu \n", (i32 & 0x0000ffff) >> 8);
printf("\nBYTE(1) of i32 = ?lu (LOW)\n", i32 & 0x000000ff);
printf("\n*p32 = ?lu \n", *p32);
// reassign new values
b32[0] = 3;
b32[1] = 4;
b32[2] = 8;
b32[3] = 2;
i32 = *p32;
// Or just use *p32 since it maps directly to the byte array.
} |
Naturally, you have to adjust interpretation for platform endianness.
I'm not sure if that answers your question. I don't think D is much different than C or C++ in this regard.
There are other ways to do it too, I'm sure.
Later,
John R. |
|