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

Papa Tana papa.tana101@REDACTED
Mon Jul 13 19:09:18 CEST 2020


CreatePDPContextRequest =
 32 10 00 bc 00 00 00 00 bc d2 00 00 02 46 06 09 21 43 65 87 F9 00 00 00 00

<<_Flags:8, _MessageType:8, _Length:16, _TEID:32, _SN:16, 0:8, 0:8,
2:8, IMSI_CODED:64, Rest/binary >> = CreatePDPContextRequest,

<<R1, R2, R3, R4, R5, R6, R7, R8>> = <<IMSI_CODEDD:64>>,	

T = [
		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] <-T ],

==> Got the Real_IMSI = string:join(R,"")

N.B: Do I really need to use lot of string function or is there a magic way?

Best Regards,


2020-07-13 19:52 UTC+03:00, Scott Ribe <scott_ribe@REDACTED>:
>> On Jul 13, 2020, at 10:46 AM, Papa Tana <papa.tana101@REDACTED> wrote:
>>
>> 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,
>
> Match on 4-bit values and put them in order?


More information about the erlang-questions mailing list