Closed metaathron closed 7 months ago
Hi @metaathron , Thanks for caching this one. I will have a look at it in a few days.
I created a PR #6, can you test it? I have tested with theses values on a Mega 2560. Encoding and decoding is OK.
Integer value | BER encoding |
---|---|
0 | 02 01 00 |
127 | 02 01 7F |
128 | 02 02 00 80 |
256 | 02 02 01 00 |
-128 | 02 01 80 |
-129 | 02 02 FF 7F |
Table 3. Example BER encodings of INTEGER values from A Layman's Guide to a Subset of ASN.1, BER, and DER
PR #6 merged. Issue solved. @metaathron , thanks a lot for your help.
You're very welcome. Also thanks for your job.
Hi again, I have found another one imperfection in your code. Negative values in IntegerBER cause infinite loop and also numbers which have higest set bit on any 8th position are represented as negative (e.g. 0000 0000 1001 0110 or 1111 1111 1011 1000)
Both can be solved by this code:
It checks if highest bit of byte is 1 (
_carry
), then it continues adding another byte to SNMP representation of number. If the number is negative, it flips it (ternary operator) and proceeds to check length which is afaik the simplest solution.