Binary to Hex String back to Binary in Erlang
A little project i'm working on in Erlang needs to take a byte sequence and write it out into a String to be save as an HTTP cookie. Then, on subsequent HTTP requests, read back in the hex string from the cookie, and convert it back into the byte sequence. I couldn't quite find a module for doing this in Erlang, so I hacked part of it, and cobbled the rest from examples I've found over the last few days.
hex(N) when N < 10 ->
hex(N) when N >= 10, N < 16 ->
int(C) when $0 =< C, C =< $9 ->
C - $0;
int(C) when $A =< C, C =< $F ->
C - $A + 10;
int(C) when $a =< C, C =< $f ->
C - $a + 10.
to_hex(N) when N < 256 ->
[hex(N div 16), hex(N rem 16)].
to_hex(H) ++ list_to_hexstr(T).
[int(X)*16 + int(Y) | hexstr_to_list(T)];