【问题标题】:Encoding and decoding ASN.1 REAL with BER使用 BER 编码和解码 ASN.1 REAL
【发布时间】:2015-01-27 08:37:04
【问题描述】:

对不起,我的英语不好。我有一个十进制数:0.15625

(这是示例)http://www.strozhevsky.com/free_docs/asn1_in_simple_words.pdf(第 5 页)

根据 BER ASN.1 的规则 - 以八进制编码:09 03 90 FE 0A(这是正确的决定)

http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf - 标准 ASN.1(8.5 - REAL)

1 字节:

(8-7) Class - Universal - 00 
(6) P/C - Primitive - 0
(5-1) Tag Number - 01001(REAL)

总计:00001001(2) = 09(16)(正确)

2 字节:

     (8) binary encoding - 1
      _____________________
     (7) When binary encoding is used (bit 8 = 1), then if the mantissa M is 
         non-zero, it shall be represented by a sign S, a positive integer value N 
         and a binary scaling factor F, such that:
            M = S × N × 2F
         Bit 7 of the first contents octets shall be 1 if S is –1 and 0 otherwise. 
         What I would have bit 7?
        _____________________
     (6-5) base 8 - 01
       _______________________
     (3-4) Bits 4 to 3 of the first contents octet shall encode the value of
     the binary scaling factor F as an unsigned binary
     integer. I don't have scaling factor. - 00
      _____________________
     (2-1) 8.5.6.4 Bits 2 to 1 of the first contents octet shall encode 
     the format of the exponent as follows: I do not know how to determine 
     what my value will be here. (Poor understand English). I think 11?

总计:1?010011 - 不等于 03 为什么? (不正确)

90是什么?调用八进制?如何找到它?书上没说,还是我根本不懂。

FE编码数字-2(指数)中,我如何翻译FE,而不是得到254和-2?也许它包含有关字节的信息:90

感谢您的收听。

【问题讨论】:

    标签: c++ c algorithm data-structures asn.1


    【解决方案1】:

    在“第 1 章。ASN.1 编码的通用规则”部分中,它指出编码包含三个部分:

    1. 信息块
    2. 一个长度块
    3. 一个值块

    长度块指定值块的长度。

    0.15625 作为八位字节09 03 80 FB 05 的编码分解如下:

    09       - information block (1 octet)
    03       - length block (1 octet)
    80 FB 05 - value block (3 octets)
    

    值块本身由三部分组成:一个信息字节、一个指数块和一个尾数块。在这种情况下,尾数为 M = 5(以 2 为底数为 101),指数为 E = -5。因此值块是:

    80       - information octet
    FB       - the exponent block (FB = -5)
    05       - the mantissa block (5)
    

    信息八位字节指定各种信息,包括:

    • 我们正在编码一个实数
    • 我们在 base 2 中使用,并且
    • 数字为非负数 (>= 0)

    要回答您关于 FE 被解释为 -2 的问题,这就是负数在 2s 补码算术 (more info) 中的表示方式。对于单字节数,我们有:

    FF   ->  -1
    FE   ->  -2
    FD   ->  -3
    ...
    80   ->  -128
    7F   ->  +127
    7E   ->  +126
    

    【讨论】:

    • 非常感谢!以及你是否有解码和编码REAL的例子?网上的资料太少了,在线编码给我增加了很多困惑。
    • 例如大于尾数和指数的两位数。
    最近更新 更多