Decoding an extravagant binary format 46 06 09 21 43 65 87 F9

Led ledest@REDACTED
Mon Jul 13 19:30:54 CEST 2020


Hi All,
>
> I receive binaries in my application:
>
> IMSI_CODED in hex_dump
> 0000   46 06 09 21 43 65 87 F9
>
> In reality, the peer is sending me this IMSI : (64 60 90 12 34 56 78 9F).
>
> I hard-decode this IMSI_CODED like below:
>
>         <<R1:8, R2:8, R3:8, R4:8, R5:8, R6:8, R7:8, R8:8>> =
> <<IMSI_CODED:64>>,
>
>         Elements = [
>                 string:right(integer_to_list(R1,16), 2, $0),
>                 string:right(integer_to_list(R2,16), 2, $0),
>                 string:right(integer_to_list(R3,16), 2, $0),
>                 string:right(integer_to_list(R4,16), 2, $0),
>                 string:right(integer_to_list(R5,16), 2, $0),
>                 string:right(integer_to_list(R6,16), 2, $0),
>                 string:right(integer_to_list(R7,16), 2, $0),
>                 string:right(integer_to_list(R8,16), 2, $0)
>                 ],
>         R = [[B,A] || [A,B] <-Elements ],       %% here, I need to revert
> each element :-/
>         L = list_to_tuple(R),
>         Z = tuple_to_list(L),
>         Real_IMSI = string:join(Z,"")
>
>
> => Real_IMSI =  "646090123456789F"
>
> I'm using the same hard-coding style to decode some SMS payload in
> binary format..
>
> But I can feel that there is a much more elegant way to do it, because
> as you can see, despite it's working, it's really dirty etc...
>
> Can anyone advice please?
>
> Thanks,
>
> Best Regards,
>

1) <<<<Y:4, X:4>> || <<X:4, Y:4>> <= Bin>>.

2) <<<<((X bsl 4) bor (X bsr 4))>> || <<X>> <= Bin>>.

-- 
Led.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20200713/760ae428/attachment.htm>


More information about the erlang-questions mailing list