Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert decimal integer to hex in Elixir

Tags:

hex

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?

like image 475
Adam Millerchip Avatar asked Jun 21 '18 03:06

Adam Millerchip


1 Answers

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"
like image 73
Adam Millerchip Avatar answered Sep 20 '22 17:09

Adam Millerchip