Permalink
Browse files

byte align bistring() to make a complementary binary()

  • Loading branch information...
1 parent d4ff30c commit 36852c73520d17c554896d003cfefef0ec40f470 @aerosol aerosol committed Sep 16, 2011
Showing with 8 additions and 2 deletions.
  1. +8 −2 src/binpp.erl
View
@@ -24,10 +24,10 @@
-spec convert(binary(), atom()) -> {ok, list()}.
-convert(Bin, hex) when is_binary(Bin) ->
+convert(Bin, hex) when is_binary(Bin) orelse is_bitstring(Bin) ->
convert(Bin, [], fun hexstr/1);
-convert(Bin, bin) when is_binary(Bin) ->
+convert(Bin, bin) when is_binary(Bin) orelse is_bitstring(Bin) ->
convert(Bin, [], fun binstr/1).
-spec convert(binary()) -> {ok, list()}.
@@ -65,6 +65,12 @@ pprint(Bin) ->
convert(<<>>, Acc, _) ->
{ok, lists:reverse(Acc)};
+%% byte align bistring() to make a complementary binary()
+convert(Bin, [], FormatFun) when is_bitstring(Bin), not is_binary(Bin) ->
+ Align = (8 - (bit_size(Bin) rem 8)),
+ io:format("Warning! Aligned bitstring with ~.10B bit(s).~n", [Align]),
+ convert(<<Bin/binary, 0:Align>>, [], FormatFun);
+
convert(<<Bin:8/integer, Rest/binary>>, SoFar, FormatFun) ->
convert(Rest, [FormatFun(Bin)|SoFar], FormatFun).

0 comments on commit 36852c7

Please sign in to comment.