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?
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With