简体   繁体   中英

How to parse ASN.1 binary data

I have a "fixed-length" binary ASN.1 encoded file with a separate documentation for the mapping of data elements. For example the binary file is of the form:

0070 00A0 1700 35e7 0100 0010 0299 1a11
bc10 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0001 0001
0256 1c41 aa09 0000 0000 0000 0000 0000
.....

Here each record is 559 bytes long. The documentation gives the record structure as the length in bytes. This format is quite specific to this data, so I need to make a converter. For example

No.     Field Name     Data Length (Bytes)   Description
1         Field1             2                 Binary
2         Field2             4                 BCD
3         Field3             1                 BCD (Right Aligned)
.....

I don't have any information if this is a standard ASN.1 encoding. The description are a bit more detailed than shown here, as they say what the decoded numbers mean. Some are binary, some BCD and some BCD (right aligned).

I don't know much about ASN.1, so I'm trying to avoid re-inventing the wheel. Is there any tool (preferably in python) that I can tweak to convert this binary file into a text CSV file? I haven't been able to find something that serves my need except this . But it's a proprietary tool and is not an option for me.

There is a module for ASN.1: pyasn1 .

Another possibility is to use the built-in struct module to separate the fields. And the answer to this question to read binary coded decimals.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM