fork download
import std.algorithm; // For reverse

void main() {
    ubyte[] littleEndian = [ 68, 135, 133, 10 ];
    auto bigEndian = littleEndian.dup;
    bigEndian.reverse();

    union Fake {
        int i;
        ubyte[4] b;
    }
    Fake f;

    f.i = 176_523_076;
    assert(f.b == littleEndian); // Machine is Little

    // Little Endian data
    f.i = little(littleEndian);
    // Converts to machine native
    assert(f.b == littleEndian);

    // Big Endian data
    f.i = big(bigEndian);
    // Converts to machine native
    assert(f.b == littleEndian);

    ubyte[] madeBig; // Want Big Endian Here
    f.b = littleEndian; // first int in Little Endian
    madeBig = machineDependant(f.i);
    assert(madeBig == bigEndian); // Converted to Big Endian
    f.b = bigEndian; // second int in Big Endian
    madeBig = machineDependant(f.i);
    assert(madeBig == bigEndian); // Fail Convert to Big Endian

}

int little(ubyte[] data) {
    return (data[0]<<0) | (data[1]<<8) | (data[2]<<16) | (data[3]<<24);
}
int big(ubyte[] data) {
    return (data[3]<<0) | (data[2]<<8) | (data[1]<<16) | (data[0]<<24);
}
ubyte[] machineDependant(int guida) {
    auto uuid = new ubyte[4];

    uuid[0] = (guida>>24)&0xff;
    uuid[1] = (guida>>16)&0xff;
    uuid[2] = (guida>>8)&0xff;
    uuid[3] = (guida>>0)&0xff;
    return uuid;
}
Runtime error #stdin #stdout 0.01s 2100KB
stdin
Standard input is empty
stdout

Standard output is empty