Skip to content

f-string {x:x}/{x:02x} on a u8 >= 0x80 prints it sign-extended (ffffffffffffffba) #1907

Closed

Description

@ehartford

Spec §15.4.1 (format specs): a hex spec formats the value; a u8 0xba is ba.

Repro (installed v0.15.3.0-g173d8aa4d and stack #1898's release binary alike):

let b: u8 = 0xba
print(f"{b:02x} {b:x} {b}")
var a: [u8; 2] = [0xba as u8, 0x10 as u8]
print(f"{a[0]:02x} {a[1]:02x}")

Actual:

ffffffffffffffba ffffffffffffffba 186
ffffffffffffffba 10

Expected:

ba ba 186
ba 10

Decimal {b} is right, so the hex path widens an unsigned byte as signed before formatting. Found while printing a SHA-256 digest (every byte >= 0x80 came out as 16 hex digits).

Activity

  1. ehartford commented on Oct 3, 2026

    @ehartford
    ContributorAuthor

    Fixed on main by 3044b9c (stack #1979, battery green on 029574d). Narrow ints carry Sema's bit width to the hex/binary/octal formatter on both the buffered and the spec path; u8..u32, i8..i64 under x/02x/#x/b/o/X.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions