Sub topic 11
Hex, binary, octal, and exponent output
This chapter assumes you know basic format strings such as {} and {:8}, and that you can run a program with cargo run. Here you will learn the type characters that change how a number is written: x, X, b, o, e, and E. You will also learn the # flag.
Type characters
The character after the colon, and after any width or precision, is the format type. An empty type means Display. ? means Debug. The other types select a trait for a numeric base or notation.
| Type | Trait | Output |
|---|---|---|
| `x` | `LowerHex` | base 16, lowercase digits |
| `X` | `UpperHex` | base 16, uppercase digits |
| `o` | `Octal` | base 8 |
| `b` | `Binary` | base 2 |
| `e` | `LowerExp` | scientific notation, lowercase `e` |
| `E` | `UpperExp` | scientific notation, uppercase `E` |
Each type calls a different trait on the value. Integers implement the radix traits. Both integers and floats implement the exponent traits.
fn main() {
let n = 255;
println!("{:x}", n);
println!("{:X}", n);
println!("{:o}", n);
println!("{:b}", n);
println!("{:e}", 1500.0_f64);
println!("{:E}", 0.00032_f64);
println!("{:e}", 1500);
}This prints ff, FF, 377, 11111111, 1.5e3, 3.2E-4, and 1.5e3. The last line shows that an integer also works with {:e}.
The `#` flag
The # flag asks for the alternate form. For radix types it adds a prefix: 0x, 0b, or 0o. For X, the digits are uppercase but the prefix stays 0x.
fn main() {
println!("{:#x}", 255);
println!("{:#X}", 255);
println!("{:#b}", 5);
println!("{:#o}", 8);
}The output is 0xff, 0xFF, 0b101, and 0o10.
Combining with width and zero padding
Width and 0 padding work with all of these types. When you use # together with 0, the width includes the prefix. The zeros go after the prefix, not before it.
fn main() {
println!("{:08b}", 5);
println!("{:#010b}", 5);
println!("{:#06x}", 255);
println!("{:02X}", 10);
}This prints 00000101, 0b00000101, 0x00ff, and 0A. A common use is printing a byte as two hex digits, as in the last line. Join several of them to build a hex dump.
Negative numbers
Radix output shows the two's complement bit pattern of a signed integer. It does not show a minus sign.
fn main() {
println!("{:b}", -5_i8);
println!("{:x}", -1_i32);
}This prints 11111011 and ffffffff. The width of the integer type decides how many digits appear. If you want a minus sign, print the value with {} instead.
Exponent output and precision
For e and E, precision sets the number of digits after the decimal point in the mantissa.
fn main() {
let big = 123456.789_f64;
println!("{:e}", big);
println!("{:.2e}", big);
println!("{:E}", big);
}This prints 1.23456789e5, 1.23e5, and 1.23456789E5. Without a precision, Rust prints as many digits as needed to represent the value exactly.
Exercise
What does println!("{:#06b}", 5); print, and why is the result six characters wide?
Show answer
It prints 0b0101. The # flag adds the 0b prefix, and the width of 6 includes that prefix. That leaves four places for digits, so the binary value 101 is padded with one zero.
Exercise
What does println!("{:x}", -1_i8); print?
Show answer
It prints ff. Radix formatting shows the two's complement bits of the signed integer. An i8 has 8 bits, so -1 is 11111111 in binary, which is ff in hex.