Skip to content

Instantly share code, notes, and snippets.

@mrinalwadhwa
Last active February 12, 2016 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrinalwadhwa/eaff91db6d4600641ab7 to your computer and use it in GitHub Desktop.
Save mrinalwadhwa/eaff91db6d4600641ab7 to your computer and use it in GitHub Desktop.
Pad a binary with zeros
-module(pad_binary).
% http://erlang.org/pipermail/erlang-questions/2008-December/040709.html
-spec pad_binary_to(WidthInBytes :: pos_integer(), Raw :: binary()) ->
Padded :: binary().
pad_binary_to(WidthInBytes, Raw) ->
case (WidthInBytes - size(Raw) rem WidthInBytes) rem WidthInBytes of
0 -> Raw;
N -> <<Raw/binary, 0:(N*8)>>
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment