Skip to content

std.fmt.hex does not work for integers that are not powers of two #23799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Noodlez1232 opened this issue May 5, 2025 · 0 comments · May be fixed by #23819
Open

std.fmt.hex does not work for integers that are not powers of two #23799

Noodlez1232 opened this issue May 5, 2025 · 0 comments · May be fixed by #23819
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@Noodlez1232
Copy link

Zig Version

0.14.0

Steps to Reproduce and Observed Behavior

Write a test.zig

const std = @import("std");
test "hex" {
    std.debug.assert(48 % 8 == 0); // multiple of u8
    const hex = std.fmt.hex(@as(u48, 0x1234_5678_3210));
    try std.testing.expectEqual(hex.len, 12);
    try std.testing.expectEqualStrings("123456783210", &hex);
}

run zig test test.zig

Get error:

thread 21610 panic: shift amount is greater than the type size
/nix/store/figbx3530fg8m6b1mfd9mvd9cpc141z3-zig-0.14.0/lib/zig/std/fmt.zig:2770:38: 0x104a88d in hex__anon_409 (test)
        const byte: u8 = @truncate(x >> @intCast(8 * i));
                                     ^
/home/nathaniel/Misc/Programs/test.zig:3:28: 0x104a681 in test.hex (test)
    const hex = std.fmt.hex(@as(u48, 0x1234_5678_3210));
                           ^
/nix/store/figbx3530fg8m6b1mfd9mvd9cpc141z3-zig-0.14.0/lib/zig/compiler/test_runner.zig:214:25: 0x10f2829 in mainTerminal (test)
        if (test_fn.func()) |_| {
                        ^
/nix/store/figbx3530fg8m6b1mfd9mvd9cpc141z3-zig-0.14.0/lib/zig/compiler/test_runner.zig:62:28: 0x10ead7d in main (test)
        return mainTerminal();
                           ^
/nix/store/figbx3530fg8m6b1mfd9mvd9cpc141z3-zig-0.14.0/lib/zig/std/start.zig:647:22: 0x10ea302 in posixCallMainAndExit (test)
            root.main();
                     ^
/nix/store/figbx3530fg8m6b1mfd9mvd9cpc141z3-zig-0.14.0/lib/zig/std/start.zig:271:5: 0x10e9edd in _start (test)
    asm volatile (switch (native_arch) {
    ^
???:?:?: 0x0 in ??? (???)

Expected Behavior

Test passes as is listed in the std documentation (since it is a multiple of u8).

@Noodlez1232 Noodlez1232 added the bug Observed behavior contradicts documented or intended behavior label May 5, 2025
myclevorname added a commit to myclevorname/zig that referenced this issue May 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant