Ad space (banner)
๐ŸŸฃJulia Lessons
Lesson 62 / 68

Converting between bases (binary and hexadecimal)

This lesson covers converting between bases in Julia, with the aim of deepening your understanding of how numbers are represented inside a computer.

string(number, base=radix) converts a number into a string in the base you specify. Conversely, parse(Int, string, base=radix) turns such a string back into a decimal number.

The sample code converts 42 to binary with string(42, base=2) and to hexadecimal with string(42, base=16), then turns the binary string back into the original number with parse(Int, "101010", base=2).

A common early stumble is the relationship between a base and the values it uses. Hexadecimal uses a–f after 0–9, and the notation takes some adjusting to from a decimal habit.

Binary is used inside the computer and hexadecimal in things like colour codes — basic knowledge worth having.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Julia directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)