'How to make Debug emit hex values in no_std code?

I'm making a bare metal application in Rust. I can easily make any given struct print its debug information, but I'd like some of them to print some of their values in hex.

For example:

impl core::fmt::Debug for Task {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_struct("Task")
            // ...
            .field("SP", &self.context.sp)
            .field("KSP", &self.context.ksp)
            // ...
            .finish()
    }
}

I cannot figure out how to make sp and ksp (both i64) print as hexadecimal numbers. My first thought was to somehow use the format! macro, but since I'm using #![no_std], I don't have access to it.

How can I make the debug output to print these values in hex?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source