View source code
Display the source code in std/bitmanip.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function std.bitmanip.write

Takes an integral value, converts it to the given endianness, and writes it to the given range of ubytes as a sequence of T.sizeof ubytes starting at index. hasSlicing!R must be true.

void write(T, Endian endianness = Endian.bigEndian, R) (
  R range,
  const T value,
  size_t index
)
if (canSwapEndianness!T && isForwardRange!R && hasSlicing!R && is(ElementType!R : ubyte));

void write(T, Endian endianness = Endian.bigEndian, R) (
  R range,
  const T value,
  size_t* index
)
if (canSwapEndianness!T && isForwardRange!R && hasSlicing!R && is(ElementType!R : ubyte));

Parameters

NameDescription
T The integral type to convert the first T.sizeof bytes to.
endianness The endianness to write the bytes in.
range The range to write to.
value The value to write.
index The index to start writing to. If index is a pointer, then it is updated to the index after the bytes read.

Example

ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0];
buffer.write!uint(29110231u, 0);
writeln(buffer); // [1, 188, 47, 215, 0, 0, 0, 0]

buffer.write!ushort(927, 0);
writeln(buffer); // [3, 159, 47, 215, 0, 0, 0, 0]

buffer.write!ubyte(42, 0);
writeln(buffer); // [42, 159, 47, 215, 0, 0, 0, 0]

Example

ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0, 0];
buffer.write!uint(142700095u, 2);
writeln(buffer); // [0, 0, 8, 129, 110, 63, 0, 0, 0]

buffer.write!ushort(19839, 2);
writeln(buffer); // [0, 0, 77, 127, 110, 63, 0, 0, 0]

buffer.write!ubyte(132, 2);
writeln(buffer); // [0, 0, 132, 127, 110, 63, 0, 0, 0]

Example

ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0];
size_t index = 0;
buffer.write!ushort(261, &index);
writeln(buffer); // [1, 5, 0, 0, 0, 0, 0, 0]
writeln(index); // 2

buffer.write!uint(369700095u, &index);
writeln(buffer); // [1, 5, 22, 9, 44, 255, 0, 0]
writeln(index); // 6

buffer.write!ubyte(8, &index);
writeln(buffer); // [1, 5, 22, 9, 44, 255, 8, 0]
writeln(index); // 7

Example

bool

ubyte[] buffer = [0, 0];
buffer.write!bool(false, 0);
writeln(buffer); // [0, 0]

buffer.write!bool(true, 0);
writeln(buffer); // [1, 0]

buffer.write!bool(true, 1);
writeln(buffer); // [1, 1]

buffer.write!bool(false, 1);
writeln(buffer); // [1, 0]

size_t index = 0;
buffer.write!bool(false, &index);
writeln(buffer); // [0, 0]
writeln(index); // 1

buffer.write!bool(true, &index);
writeln(buffer); // [0, 1]
writeln(index); // 2

Example

char(8-bit)

ubyte[] buffer = [0, 0, 0];

buffer.write!char('a', 0);
writeln(buffer); // [97, 0, 0]

buffer.write!char('b', 1);
writeln(buffer); // [97, 98, 0]

size_t index = 0;
buffer.write!char('a', &index);
writeln(buffer); // [97, 98, 0]
writeln(index); // 1

buffer.write!char('b', &index);
writeln(buffer); // [97, 98, 0]
writeln(index); // 2

buffer.write!char('c', &index);
writeln(buffer); // [97, 98, 99]
writeln(index); // 3

Example

wchar (16bit - 2x ubyte)

ubyte[] buffer = [0, 0, 0, 0];

buffer.write!wchar('ą', 0);
writeln(buffer); // [1, 5, 0, 0]

buffer.write!wchar('”', 2);
writeln(buffer); // [1, 5, 32, 29]

size_t index = 0;
buffer.write!wchar('ć', &index);
writeln(buffer); // [1, 7, 32, 29]
writeln(index); // 2

buffer.write!wchar('ą', &index);
writeln(buffer); // [1, 7, 1, 5]
writeln(index); // 4

Example

dchar (32bit - 4x ubyte)

ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0];

buffer.write!dchar('ą', 0);
writeln(buffer); // [0, 0, 1, 5, 0, 0, 0, 0]

buffer.write!dchar('”', 4);
writeln(buffer); // [0, 0, 1, 5, 0, 0, 32, 29]

size_t index = 0;
buffer.write!dchar('ć', &index);
writeln(buffer); // [0, 0, 1, 7, 0, 0, 32, 29]
writeln(index); // 4

buffer.write!dchar('ą', &index);
writeln(buffer); // [0, 0, 1, 7, 0, 0, 1, 5]
writeln(index); // 8

Example

float (32bit - 4x ubyte)

ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0];

buffer.write!float(32.0f, 0);
writeln(buffer); // [66, 0, 0, 0, 0, 0, 0, 0]

buffer.write!float(25.0f, 4);
writeln(buffer); // [66, 0, 0, 0, 65, 200, 0, 0]

size_t index = 0;
buffer.write!float(25.0f, &index);
writeln(buffer); // [65, 200, 0, 0, 65, 200, 0, 0]
writeln(index); // 4

buffer.write!float(32.0f, &index);
writeln(buffer); // [65, 200, 0, 0, 66, 0, 0, 0]
writeln(index); // 8

Example

double (64bit - 8x ubyte)

ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

buffer.write!double(32.0, 0);
writeln(buffer); // [64, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

buffer.write!double(25.0, 8);
writeln(buffer); // [64, 64, 0, 0, 0, 0, 0, 0, 64, 57, 0, 0, 0, 0, 0, 0]

size_t index = 0;
buffer.write!double(25.0, &index);
writeln(buffer); // [64, 57, 0, 0, 0, 0, 0, 0, 64, 57, 0, 0, 0, 0, 0, 0]
writeln(index); // 8

buffer.write!double(32.0, &index);
writeln(buffer); // [64, 57, 0, 0, 0, 0, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0]
writeln(index); // 16

Example

enum

ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

enum Foo
{
    one = 10,
    two = 20,
    three = 30
}

buffer.write!Foo(Foo.one, 0);
writeln(buffer); // [0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0]

buffer.write!Foo(Foo.two, 4);
writeln(buffer); // [0, 0, 0, 10, 0, 0, 0, 20, 0, 0, 0, 0]

buffer.write!Foo(Foo.three, 8);
writeln(buffer); // [0, 0, 0, 10, 0, 0, 0, 20, 0, 0, 0, 30]

size_t index = 0;
buffer.write!Foo(Foo.three, &index);
writeln(buffer); // [0, 0, 0, 30, 0, 0, 0, 20, 0, 0, 0, 30]
writeln(index); // 4

buffer.write!Foo(Foo.one, &index);
writeln(buffer); // [0, 0, 0, 30, 0, 0, 0, 10, 0, 0, 0, 30]
writeln(index); // 8

buffer.write!Foo(Foo.two, &index);
writeln(buffer); // [0, 0, 0, 30, 0, 0, 0, 10, 0, 0, 0, 20]
writeln(index); // 12

Example

enum - float

ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0];

enum Float: float
{
    one = 32.0f,
    two = 25.0f
}

buffer.write!Float(Float.one, 0);
writeln(buffer); // [66, 0, 0, 0, 0, 0, 0, 0]

buffer.write!Float(Float.two, 4);
writeln(buffer); // [66, 0, 0, 0, 65, 200, 0, 0]

size_t index = 0;
buffer.write!Float(Float.two, &index);
writeln(buffer); // [65, 200, 0, 0, 65, 200, 0, 0]
writeln(index); // 4

buffer.write!Float(Float.one, &index);
writeln(buffer); // [65, 200, 0, 0, 66, 0, 0, 0]
writeln(index); // 8

Example

enum - double

ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

enum Double: double
{
    one = 32.0,
    two = 25.0
}

buffer.write!Double(Double.one, 0);
writeln(buffer); // [64, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

buffer.write!Double(Double.two, 8);
writeln(buffer); // [64, 64, 0, 0, 0, 0, 0, 0, 64, 57, 0, 0, 0, 0, 0, 0]

size_t index = 0;
buffer.write!Double(Double.two, &index);
writeln(buffer); // [64, 57, 0, 0, 0, 0, 0, 0, 64, 57, 0, 0, 0, 0, 0, 0]
writeln(index); // 8

buffer.write!Double(Double.one, &index);
writeln(buffer); // [64, 57, 0, 0, 0, 0, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0]
writeln(index); // 16

Example

enum - real

ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

enum Real: real
{
    one = 32.0,
    two = 25.0
}

static assert(!__traits(compiles, buffer.write!Real(Real.one)));

Authors

Walter Bright, Andrei Alexandrescu, Jonathan M Davis, Alex Rønne Petersen, Damian Ziemba, Amaury SECHET

License

Boost License 1.0.