bigEndian in std.bitmanip

Salih Dincer salihdb at hotmail.com
Tue Oct 31 14:23:28 UTC 2023


On Tuesday, 31 October 2023 at 10:24:56 UTC, Jonathan M Davis 
wrote:
> On Tuesday, October 31, 2023 4:09:53 AM MDT Salih Dincer via 
> Digitalmars-d- learn wrote:
>> Hello,
>>
>> Why isn't Endian.littleEndian the default setting for read() in
>> std.bitmanip?
>
> Why would you expect little endian to be the default? The 
> typical thing to do when encoding integral values in a 
> platform-agnostic manner is to use big endian, not little 
> endian...

Because when we create a structure with a Union, it does reverse 
insertion with according to the static array(bytes) index; I 
showed this above.  I also have a convenience template like this:

```d
template readBytes(T, bool big = false, R)
{        // pair endian version 2.0
   import bop = std.bitmanip;

   static if(big)
     enum E = bop.Endian.bigEndian;
   else
     enum E = bop.Endian.littleEndian;

   auto readBytes(ref R dat)
    => bop.read!(T, E)(dat);
}
```
Sorry to give you extra engage because I already solved the 
problem with readBytes(). Thank you for your answer, but there is 
1 more problem, or even 2! The read() in the library, which is 
2nd function, conflicts with std.write. Yeah, there are many 
solutions to this, but what it does is just read bytes. However, 
you can insert 4 ushorts into one ulong.

Don't you think the name of the function should be readBytes, not 
read?  Because it doesn't work with any type other than ubyte[]!

SDB at 79



More information about the Digitalmars-d-learn mailing list