On Tuesday, 31 October 2023 at 10:09:53 UTC, Salih Dincer wrote:
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@79
It might make sense to change since little endian is the most
common when it comes to hardware. But big endian is most common
when it comes to networking. So I guess it depends on your view
of what is most common. Interacting with your local hardware or
networking.