Decode a trail of <<Header,Length,Payload,......,Header,Length, Payload,.......>> from Binary Input
Papa Tana
papa.tana101@REDACTED
Tue Jul 14 21:47:55 CEST 2020
Dear All,
I am working with some Binary Input as below, which complete format is:
Data =
<<
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Header | LENGTH(8bits) | Payload |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Header | LENGTH(16bits) | Payload|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Header | LENGTH(32bits) | Payload|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Header | LENGTH(8bits) | Payload |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Header | LENGTH(32bits) | Payload|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Header | LENGTH(16bits) | Payload|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Header | LENGTH(8bits) | Payload |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Header | LENGTH(16bits) | Payload|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
>>
- Header is always 8 bits,
- LENGTH has a dynamic size(can be 8 "if Header=1", 16 "if Header=2",
24 "if Header=3" or 32 "if Header=4")
- Payload is deducted from LENGTH
I am doing something like this in my Code(repetitive check), because I
have to test Data for every possible value of Header (impossible to
maintain such a code):
[ {Header, LENGTH, Payload} || <<Header:8, LENGTH:8 if Header = 1,
Payload:LENGTH*8>> <= Data ]
[ {Header, LENGTH, Payload} || <<Header:8, LENGTH:16 if Header = 2,
Payload:LENGTH*8>> <= Data ]
[ {Header, LENGTH, Payload} || <<Header:8, LENGTH:24 if Header = 3,
Payload:LENGTH*8>> <= Data ]
[ {Header, LENGTH, Payload} || <<Header:8, LENGTH:32 if Header = 4,
Payload:LENGTH*8>> <= Data ]
Is there any way to extract all {Header, LENGTH, Payload} in this Data
Binary Input in a more efficient way please?
An example of Data:
Data =
<<
1,2,"a","b",
3,1,"x",
4,1,"y",
8,2,"m", "n"
>>.
Thanks in advance,
Best Regards,
More information about the erlang-questions
mailing list