bit twiddling (endianness)

Stewart Gordon smjg_1998 at yahoo.com
Wed Jan 10 19:22:12 PST 2007


Lutger wrote:
> Hi, I need to convert a uint to 4 ubytes in big endian order for which I 
> have written the function below. The thing is, I'm not 100% sure it is 
> correct for Big Endian systems, would somebody be so kind as to inform 
> me if this will work?
> 
> void concatUint(inout ubyte[] bytestream, uint num)
> {
>     bytestream.length = bytestream.length + 4;
>     static if (std.system.endian == Endian.LittleEndian)

torhu is right - your code doesn't need to interrogate the platform byte 
order.

But for future reference, if you ever do want to write endian-dependent 
code, you might version (LittleEndian) and version (BigEndian) nicer 
than examining std.system.endian.

Stewart.


More information about the Digitalmars-d-learn mailing list