I have a packet of hexadecimal values that I'm trying to deal with. They come in as a string. For example, one piece of the packet is C0
, which is 192
. However, I'm not quite sure how to translate the string value into the integer value.
If I use this:
Base.decode16!("C0")
# <<192>>
... I get a binary.
The only way I can think to extract this integer value is like so:
<<x>> = Base.decode16!("C0")
x
# 192
This works, and it seems sort of idiomatic, but I'm new to Elixir and bit unsure if this is the best solution. How would you go about translating a string hex value into an integer in Elixir?
You can use Integer
Integer.parse("C0", 16) # returns {192, ""}
To convert it back you can use
# to charlist
Integer.to_charlist(192, 16) # returns 'C0'
# to string
Integer.to_string(192, 16) # returns "C0"
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