order

Swaps the bytes to the given ordering but does a no-op if the ordering requested is the same as that of the system's

T
order
(
T
)
if (
__traits(isIntegral, T)
)

Parameters

bytesIn T

the integral value to swap

order Order

the byte ordering to request

Return Value

Type: T

the integral value

Examples

Tests the order!(T)(T, Order)

To Big Endian testing

version(LittleEndian)
{
    ushort i = 1;
    writeln("Pre-order: ", i);
    ushort ordered = order(i, Order.BE);
    writeln("Post-order: ", ordered);
    assert(ordered == 256);
}
else version(BigEndian)
{
    ushort i = 1;
    writeln("Pre-order: ", i);
    ushort ordered = order(i, Order.BE);
    writeln("Post-order: ", ordered);
    assert(ordered == i);
}

Tests the order!(T)(T, Order)

To Little Endian testing

version(LittleEndian)
{
    ushort i = 1;
    writeln("Pre-order: ", i);
    ushort ordered = order(i, Order.LE);
    writeln("Post-order: ", ordered);
    assert(ordered == i);
}
else version(BigEndian)
{
    ushort i = 1;
    writeln("Pre-order: ", i);
    ushort ordered = order(i, Order.LE);
    writeln("Post-order: ", ordered);
    assert(ordered == 256);
}

Meta