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

Oliver Korpilla Oliver.Korpilla@REDACTED
Mon Jul 13 19:11:10 CEST 2020


Hi,
 
instead of matching out each byte, you could use binary_to_list, I think it would give you a list of octets, IIRC, skipping building the list of elements by hand. Then you could map.
 

 
string:join([ << Y, Z >> || <<X:4, Y:4>> <= <<IMSI_CODED:64>> ], "").
 
Could somehing like this work?

I have no Erlang interpreter to test it.
 
Oliver
 
 

Gesendet: Montag, 13. Juli 2020 um 18:46 Uhr
Von: "Papa Tana" <papa.tana101@REDACTED>
An: "erlang-questions" <erlang-questions@REDACTED>
Betreff: 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,


More information about the erlang-questions mailing list