11

I have the following value in Elixir: <<140, 143, 153, 192, 237, 255, 10>> Binaries don't seem to be enumerable. I need to convert it to a list so I can iterate over it, byte-by-byte, something like: [140, 143, 153, 192, 237, 255, 10]. I understand that to_char_array would do it if all the bytes were valid unicode characters but they aren't.

Just getting started with Elixir and really appreciate any suggestions for converting binaries to lists(byte arrays).

1
  • 4
    I just discovered this approach: byte_array = for <<x::8 <- binary_data>>, do: x This seems to work but would be interested to see any other suggestions. Thanks! Commented May 27, 2016 at 14:28

1 Answer 1

24

Have a look at erlang's bin_to_list/1

:binary.bin_to_list(<<140, 143, 153, 192, 237, 255, 10>>)
# [140, 143, 153, 192, 237, 255, 10]
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, and much more readable than my initial solution. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.