1const std = @import("std");
2const string = []const u8;
3const extras = @import("./lib.zig");
4const builtin = @import("builtin");
5const native_endian = builtin.target.cpu.arch.endian();
6
7pub fn rawInt(comptime T: type, comptime literal: comptime_int) T {
8 comptime std.debug.assert(@typeInfo(T).int.bits % 8 == 0);
9 return switch (native_endian) {
10 .little => @byteSwap(@as(T, literal)),
11 .big => @compileError("unreachable"),
12 };
13}
14
15test {
16 const bytes = std.mem.toBytes(rawInt(u16, 0x4e5a));
17 try std.testing.expect(bytes[0] == 0x4e);
18}
19test {
20 const bytes = std.mem.toBytes(rawInt(u16, 0x4e5a));
21 try std.testing.expect(bytes[0] == 0x4e);
22 try std.testing.expect(bytes[1] == 0x5a);
23}
24test {
25 const bytes = std.mem.toBytes(rawInt(u32, 0x4e5a7da9));
26 try std.testing.expect(bytes[0] == 0x4e);
27 try std.testing.expect(bytes[1] == 0x5a);
28 try std.testing.expect(bytes[2] == 0x7d);
29 try std.testing.expect(bytes[3] == 0xa9);
30}
31test {
32 const bytes = std.mem.toBytes(rawInt(u64, 0x4e5a7da9f3f1d132));
33 try std.testing.expect(bytes[0] == 0x4e);
34 try std.testing.expect(bytes[1] == 0x5a);
35 try std.testing.expect(bytes[2] == 0x7d);
36 try std.testing.expect(bytes[3] == 0xa9);
37 try std.testing.expect(bytes[4] == 0xf3);
38 try std.testing.expect(bytes[5] == 0xf1);
39 try std.testing.expect(bytes[6] == 0xd1);
40 try std.testing.expect(bytes[7] == 0x32);
41}