Skip to content

Instantly share code, notes, and snippets.

@speeddragon
Created September 13, 2023 18:03
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 speeddragon/b858f5ded9dcef7d085e9a7e87a8b347 to your computer and use it in GitHub Desktop.
Save speeddragon/b858f5ded9dcef7d085e9a7e87a8b347 to your computer and use it in GitHub Desktop.
Split binary file by odd/even bytes in Elixir
{:ok, binary} = File.read("original.bin")
{f1, f2} = binary
|> :binary.bin_to_list()
|> Enum.reduce({"", ""}, fn hex, {f1, f2} ->
if byte_size(f1) != byte_size(f2) do
{f1, f2 <> <<hex>>}
else
{f1 <> <<hex>>, f2}
end
end)
File.write("original_split1.bin", f1, [:binary, :write])
File.write("original_split2.bin", f2, [:binary, :write])
IO.puts("Done!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment