Necrobious'

Tuesday, March 04, 2008

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.

Enjoy!



-module(hex).
-export([bin_to_hexstr/1,hexstr_to_bin/1]).

hex(N) when N < 10 ->
$0+N;
hex(N) when N >= 10, N < 16 ->
$a+(N-10).

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)].

list_to_hexstr([]) ->
[];
list_to_hexstr([H|T]) ->
to_hex(H) ++ list_to_hexstr(T).

bin_to_hexstr(Bin) ->
list_to_hexstr(binary_to_list(Bin)).

hexstr_to_bin(S) ->
list_to_binary(hexstr_to_list(S)).

hexstr_to_list([X,Y|T]) ->
[int(X)*16 + int(Y) | hexstr_to_list(T)];
hexstr_to_list([]) ->
[].