Search code examples
erlanghex

Using Hexadecimal in Erlang


Is there any way to convert a list of hexadecimal character to a binary that corresponds to the hexadecimal coding?

Example:

[FF,AC,01]=><<255,172,1>>

Solution

  • I guess you meant this: ["FF","AC","01"] => <<255,172,1>>.

    You can use list_to_integer/2 function. It takes number base as second argument.

    Hexs = ["FF","AC","01"],
    Ints = [list_to_integer(Hex, 16) || Hex <- Hexs],
    %% [255,172,1]
    Binary = list_to_binary(Ints).
    %% <<255,172,1>>