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

Oliver Korpilla Oliver.Korpilla@REDACTED
Mon Jul 13 19:32:14 CEST 2020


 
    A = [ [Y, X] || << X:4, Y:4 >> <= << 0, 1, 2, 3, 129, 254 >>],
    B = lists:flatten(A),
    C = lists:map(fun (X) -> integer_to_list(X, 16) end, B),
    string:join(C, "").
 

Gesendet: Montag, 13. Juli 2020 um 19:30 Uhr
Von: "Led" <ledest@REDACTED>
An: "erlang-questions" <erlang-questions@REDACTED>
Betreff: Re: Decoding an extravagant binary format 46 06 09 21 43 65 87 F9

 

 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.


More information about the erlang-questions mailing list