Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C with Coccinelle
C++ with Coccinelle
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Mojo
Nim
Numba
Nix
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
PTX
Python
Racket
Raku
Ruby
Rust
Sail
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
Triton
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
sway
zig source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
zig 0.10.0
zig 0.11.0
zig 0.12.0
zig 0.12.1
zig 0.13.0
zig 0.14.0
zig 0.14.1
zig 0.15.1
zig 0.2.0
zig 0.3.0
zig 0.4.0
zig 0.5.0
zig 0.6.0
zig 0.7.0
zig 0.7.1
zig 0.8.0
zig 0.9.0
zig trunk
Options
Source code
const std = @import("std"); const t = std.testing; pub const LobbyEnter_t = extern struct { m_ulSteamIDLobby: u64 = 0, m_rgfChatPermissions: u32 = 0, m_bLocked: bool = false, m_EChatRoomEnterResponse: u32 = 0, }; pub const PersonaStateChange_t = extern struct { m_ulSteamID: u64 = 0, m_nChangeFlags: i32 = 0, }; pub const LobbyCreated_t = extern struct { m_eResult: c_int = 0, m_ulSteamIDLobby: u64 = 0, }; pub const LobbyDataUpdate_t = extern struct { m_ulSteamIDLobby: u64 = 0, m_ulSteamIDMember: u64 = 0, m_bSuccess: u8 = 0, }; pub fn main() !void { try test_serializer( LobbyCreated_t{ .m_eResult = 1, .m_ulSteamIDLobby = 109775242231394386 }, "0100000052b0554e00008601", ); try test_serializer( PersonaStateChange_t{ .m_ulSteamID = 76561197998998680, .m_nChangeFlags = 32 }, "98044f020100100120000000", ); try test_serializer( LobbyDataUpdate_t{ .m_ulSteamIDLobby = 109775242231394386, .m_ulSteamIDMember = 109775242231394386, .m_bSuccess = 1 }, "52b0554e0000860152b0554e0000860101732060", ); try test_serializer( LobbyEnter_t{ .m_ulSteamIDLobby = 109775242231394386, .m_rgfChatPermissions = 0, .m_bLocked = false, .m_EChatRoomEnterResponse = 19875317 }, "52b0554e000086010000000000f5452f01000000", ); } pub fn from_slice(comptime T: anytype, slice: []const u8) T { var ret: T = std.mem.zeroes(T); var retP = &ret; const struct_info = @typeInfo(T).Struct; var start: usize = 0; // option A: copy the slice of memory (Doesnt work) // ret = @as(*T, @ptrCast(@alignCast(slice))).*; // option B: comptime-reflect the fields and copy the memory of each element inline for (struct_info.fields) |field| { if (!field.is_comptime) { const end = start + @sizeOf(field.type); if (end > slice.len) @panic("overflow"); std.debug.print(" ret[{s}] = slice[{d}..{d} ({d})] = {s} offset_in_c={d} offset_in_zig={d}\n", .{ field.name, start, end, @sizeOf(field.type), std.fmt.fmtSliceHexLower(slice[start..end]), start, @offsetOf(T, field.name), }); // copy the slice of memory corresponding to the field @memcpy(std.mem.asBytes(&@field(ret, field.name)), slice[start..end]); start = end; } } std.debug.print(" ret = {any}\n", .{ret}); std.debug.print("memory dump (zig): {}\n memory dump (c): {}\n", .{ std.fmt.fmtSliceHexLower(std.mem.asBytes(retP)), std.fmt.fmtSliceHexLower(slice), }); return ret; } ////////////////// helpers fn test_serializer(value: anytype, comptime slice: []const u8) !void { var bytes: [slice.len / 2]u8 = std.mem.zeroes([slice.len / 2]u8); for (slice, 0..) |char, index| { var shift: u3 = if (@rem(index, 2) == 0) 4 else 0; var pos: usize = @divFloor(index, 2); bytes[pos] = bytes[pos] | try charToInt(char) << shift; } std.debug.print("\nWill create a type {s} using the bytes {s}:\n", .{@typeName(@TypeOf(value)), slice}); try t.expectEqualDeep(value, from_slice(@TypeOf(value), &bytes)); } fn charToInt(c: u8) !u8 { return switch (c) { '0'...'9' => c - '0', 'A'...'F' => c - 'A' + 10, 'a'...'f' => c - 'a' + 10, else => @panic("invalid char"), }; }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Compiler Explorer Shop
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
Statistics
Changelog
Version tree