Skip to content

Instantly share code, notes, and snippets.

@archaelus
Created December 21, 2008 20:44
Show Gist options
  • Save archaelus/38762 to your computer and use it in GitHub Desktop.
Save archaelus/38762 to your computer and use it in GitHub Desktop.
%%%-------------------------------------------------------------------
%% @copyright Geoff Cant
%% @author Geoff Cant <[email protected]>
%% @version {@vsn}, {@date} {@time}
%% @doc Wireshark hex byte stream -> erlang binary
%% @end
%%%-------------------------------------------------------------------
-module(wshark).
-include_lib("eunit/include/eunit.hrl").
%% API
-export([decode/1]).
%%====================================================================
%% API
%%====================================================================
decode(Bytes) when is_list(Bytes) ->
decode(Bytes, <<>>).
decode([], Acc) -> Acc;
decode([A,B | Rest], Bin) ->
{ok, [N], []} = io_lib:fread("~16u", [A,B]),
decode(Rest, <<Bin/binary, N:8/little>>).
decode_test() ->
?assertMatch(<<62,0,0,1,165,162,0,0,0,0,0,64,8,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,101,106,97,98,98,101,114,100,0,20,
250,250,28,1,178,179,188,240,224,6,40,213,32,38,142,14,
69,236,160,21>>,
decode("3e000001a5a2000000000040080000000000000000000000000000000"
"000000000000000656a6162626572640014fafa1c01b2b3bcf0e00628"
"d520268e0e45eca015")).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment