E_net4
E_net4

Reputation: 30003

How do I format a signed integer to a sign-aware hexadecimal representation?

My initial intent was to convert a signed primitive number to its hexadecimal representation in a way that preserves the number's sign. It turns out that the current implementations of LowerHex, UpperHex and relatives for signed primitive integers will simply treat them as unsigned. Regardless of what extra formatting flags that I add, these implementations appear to simply reinterpret the number as its unsigned counterpart for formatting purposes. (Playground)

println!("{:X}", 15i32);           // F
println!("{:X}", -15i32);          // FFFFFFF1   (expected "-F")
println!("{:X}", -0x80000000i32);  // 80000000   (expected "-80000000")
println!("{:+X}", -0x80000000i32); // +80000000
println!("{:+o}", -0x8000i16);     // +100000
println!("{:+b}", -0x8000i16);     // +1000000000000000

The documentation in std::fmt is not clear on whether this is supposed to happen, or is even valid, and UpperHex (or any other formatting trait) does not mention that the implementations for signed integers interpret the numbers as unsigned. There seem to be no related issues on Rust's GitHub repository either. (Post-addendum notice: Starting from 1.24.0, the documentation has been improved to properly address these concerns, see issue #42860)

Ultimately, one could implement specific functions for the task (as below), with the unfortunate downside of not being very compatible with the formatter API.

fn to_signed_hex(n: i32) -> String {
    if n < 0 {
        format!("-{:X}", -n)
    } else {
        format!("{:X}", n)
    }
}

assert_eq!(to_signed_hex(-15i32), "-F".to_string());

Is this behaviour for signed integer types intentional? Is there a way to do this formatting procedure while still adhering to a standard Formatter?

Upvotes: 7

Views: 1412

Answers (2)

Craig McQueen
Craig McQueen

Reputation: 43446

This is like Francis Gagné's answer, but made generic to handle i8 through i128.

use std::fmt::{self, Formatter, UpperHex};
use num_traits::Signed;

struct ReallySigned<T: PartialOrd + Signed + UpperHex>(T);

impl<T: PartialOrd + Signed + UpperHex> UpperHex for ReallySigned<T> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        let prefix = if f.alternate() { "0x" } else { "" };
        let bare_hex = format!("{:X}", self.0.abs());
        f.pad_integral(self.0 >= T::zero(), prefix, &bare_hex)
    }
}

fn main() {
    println!("{:#X}", -0x12345678);
    println!("{:#X}", ReallySigned(-0x12345678));
}

Upvotes: 2

Francis Gagn&#233;
Francis Gagn&#233;

Reputation: 65782

Is there a way to do this formatting procedure while still adhering to a standard Formatter?

Yes, but you need to make a newtype in order to provide a distinct implementation of UpperHex. Here's an implementation that respects the +, # and 0 flags (and possibly more, I haven't tested):

use std::fmt::{self, Formatter, UpperHex};

struct ReallySigned(i32);

impl UpperHex for ReallySigned {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        let prefix = if f.alternate() { "0x" } else { "" };
        let bare_hex = format!("{:X}", self.0.abs());
        f.pad_integral(self.0 >= 0, prefix, &bare_hex)
    }
}

fn main() {
    for &v in &[15, -15] {
        for &v in &[&v as &UpperHex, &ReallySigned(v) as &UpperHex] {
            println!("Value: {:X}", v);
            println!("Value: {:08X}", v);
            println!("Value: {:+08X}", v);
            println!("Value: {:#08X}", v);
            println!("Value: {:+#08X}", v);
            println!();
        }
    }
}

Upvotes: 7

Related Questions