liveBook · Manning

Hexadecimal number

As explained in Rust in Action: Systems programming concepts and techniques

Hexadecimal notation is a base-16 positional numeral system that represents values using sixteen distinct digits.

Hexadecimal is a base-16 positional numeral system. It uses sixteen digit values: 0–9 and, conventionally, a–f. In Rust, hexadecimal integer literals begin with 0x, so 0x12C represents 300: 1 × 256 + 2 × 16 + 12. section 2.3.2

Writing and formatting hexadecimal values

Rust accepts hexadecimal literals alongside binary and octal literals: let three_hundred = 0x12C; println!("decimal: {}", three_hundred); println!("hexadecimal: {:x}", three_hundred); The value prints as 300 in decimal and 12c in hexadecimal. The same integer can therefore have different written representations without changing its value. section 2.3.2

Hexadecimal is especially useful for viewing bytes. Two hexadecimal digits represent every value from 0x00 through 0xff, covering all 256 possible bit patterns in one byte. A hexdump displays file data this way, pairing each byte with an address such as [0x00000000]. Values such as 0x0a can also reveal control characters such as newline. section 7.3

Listing 2.4 Using base 2, base 8, and base 16 numeric literals
 fn main() {
   let three = 0b11;             #1
   let thirty = 0o36;            #2
   let three_hundred = 0x12C;    #3
 
   println!("base 10: {} {} {}", three, thirty, three_hundred);
   println!("base 2:  {:b} {:b} {:b}", three, thirty, three_hundred);
   println!("base 8:  {:o} {:o} {:o}", three, thirty, three_hundred);
   println!("base 16: {:x} {:x} {:x}", three, thirty, three_hundred);
 }

Compare binary, octal, decimal, and hexadecimal literals and observe how Rust prints the same values in each base. section 2.3.2

Table 7.2 fview in operation (view table figure)

fview input

fn main() {
println!("Hello, world!");
}

fview output

[0x00000000] 0a 66 6e 20 6d 61 69 6e 28 29 20 7b 0a 20 20 20
[0x00000010] 20 70 72 69 6e 74 6c 6e 21 28 22 48 65 6c 6c 6f
[0x00000020] 2c 20 77 6f 72 6c 64 21 22 29 3b 0a 7d

Inspect a concrete hexdump of a short Rust source file, including byte addresses and hexadecimal byte pairs. section 7.3

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Explains base-16 conversion, Rust syntax, byte coverage, and practical output, then stops short of a comprehensive numeral-system reference.
Examples
Demonstrates 0x12C for 300, Rust formatting, fview output, byte markers, and a CHIP-8 program written directly in hexadecimal.
What it assumes
Assumes basic integer values while teaching binary, octal, and hexadecimal conversion through concrete numerical comparisons.
What you can do after
Apply it via Rust literals and formatting, inspect file bytes with a hexdump, and understand hexadecimal CPU programs.
How it connects
Connects hexadecimal with positional notation, integer types, bytes, file I/O, hexdumps, CPU instructions, pointers, and the stack.

Also explained in

Compare how 5 books explain hexadecimal number →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.