[erlang-questions] Reverses Order of Bytes in Binary

José Valim <>
Fri Jan 9 15:04:01 CET 2015


If you have a small amount of bytes, nox showed an interesting solution a
couple days ago that relies on endianness:

1> <<I:32/integer>> = <<1, 2, 3, 4>>.
<<1,2,3,4>>
2> <<I:32/little>>.
<<4,3,2,1>>





*José Valim*
www.plataformatec.com.br
Skype: jv.ptec
Founder and Lead Developer

On Fri, Jan 9, 2015 at 2:54 PM, Harit Himanshu <
> wrote:

> This is another exercise in Programming Edition book that talk about
>
> reverses the order of bytes in a binary
>
> So my attempt looks like
>
> 58> B = <<1,2,3,4>>.
>
> <<1,2,3,4>>
>
> 59> binary_to_list(B).
>
> [1,2,3,4]
>
> 60> lists:reverse(binary_to_list(B)).
>
> [4,3,2,1]
>
> 61> list_to_binary(lists:reverse(binary_to_list(B))).
>
> <<4,3,2,1>>
>
> 62>
>
>
> Wondering if there is a better way available. Any recommendations?
>
> Thank you
> + Harit Himanshu
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150109/f6db4a43/attachment.htm>


More information about the erlang-questions mailing list