Adam Millerchip
Adam Millerchip

Reputation: 23147

Convert decimal integer to hex in Elixir

I know we can declare an integer in base 2, 8, 10, or 16, for example:

0b10000
0o20
16
0x10

all result in the integer 16.

But given an integer, for example 43981, how do I get its hexadecimal representation?

Upvotes: 9

Views: 5259

Answers (1)

Adam Millerchip
Adam Millerchip

Reputation: 23147

Use Integer.to_string/2 with 16 as the second argument.

Integer.to_string(43981, 16) # "ABCD"

You can also get the binary and octal representations the same way:

Integer.to_string(43981, 2) # "1010101111001101"
Integer.to_string(43981, 8) # "125715"

Upvotes: 16

Related Questions