bigEndian in std.bitmanip

Salih Dincer salihdb at hotmail.com
Tue Oct 31 10:09:53 UTC 2023


Hello,

Why isn't Endian.littleEndian the default setting for read() in 
std.bitmanip?

Okay, we can easily change this if we want (I could use enum LE 
in the example) and I can also be reversed with 
data.retro.array().

```d
void main()
{
   import std.conv : hexString;
   string helloD = hexString!"48656C6C6F204421";
   // compile time converted literal string -ˆ

   import std.string : format;
   auto hexF = helloD.format!"%(%02X%)";

   import std.digest: toHexString;
   auto arr = cast(ubyte[])"Hello D!";

   auto hex = arr.toHexString;
   assert(hex == hexF);

   import std.stdio : writeln;
   hex.writeln(": ", helloD);
// 48656C6C6F204421: Hello D!
   assert(helloD == "Hello D!");

   auto data = arr.readBytes!size_t;
   data.code.writeln(": ", data.bytes);
// 2397076564600448328: Hello D!
}

template readBytes(T, R)
{
   union Bytes
   {
     T code;
     char[T.sizeof] bytes;
   }
   import std.bitmanip;
   enum LE = Endian.littleEndian;

   auto readBytes(ref R data)
   {
    import std.range : retro, array;
    auto reverse = data.retro.array;
    return Bytes(reverse.read!T);
   }
}
```

However, I think it is not compatible with Union. Thanks...

SDB at 79


More information about the Digitalmars-d-learn mailing list