LittleEndian

Represents a little endian type. Most significant bits come last, so 0x4000 is, for example, represented as [00, 04].

alias LittleEndian(T) = EndianType!(T, true)

Examples

import std.conv : text;
LittleEndian!ushort x;
x = cast(ubyte[])[0, 2];
assert(x == 0x200);
ushort tmp;
x.native(tmp);
assert(tmp == 512);
assert(x.text == "512");
ubyte[] z = [0, 3];
x = z;
assert(x == 0x300);
assert(x.text == "768");
x = 1024;
assert(x.raw == [0, 4]);

LittleEndian!float f;
f = cast(ubyte[])[0, 0, 32, 64];
assert(f == 2.5);

align(1) static struct Test {
	align(1):
	uint a;
	ushort b;
	ushort[2] c;
}
LittleEndian!Test t;
t = cast(ubyte[])[3, 2, 1, 0, 2, 1, 6, 5, 8, 7];
assert(t.a == 0x010203);
assert(t.b == 0x0102);
assert(t.c == [0x0506, 0x0708]);
t = Test(42, 42, [10, 20]);
assert(t.raw == [42, 0, 0, 0, 42, 0, 10, 0, 20, 0]);

align(1) static struct Test2 {
	align(1):
	ubyte a;
	char b;
	ubyte[4] c;
}
LittleEndian!Test2 t2;
t2 = cast(ubyte[])[20, 30, 1, 2, 3, 4];
assert(t2.a == 20);
assert(t2.b == 30);
assert(t2.c == [1, 2, 3, 4]);

Meta