Numerical information Encoding

INTEGER

Integer is encoded as a two's complement binary number. In this representation the most significant bit (MSB) has weight of -(2m-1), all other bits have positive one. Thus with two octets (16bit) is possible to represent numbers ranging from -32768 to 32767. Let's have a following definition:

It would be then encoded as:

But it cannot be encoded as

although it is the same value in 32bit word, because the number can occupy only minimum required octets.


REAL

This type is somewhat more complex, that's why there are four ways of encoding:

The value of zero is encoded by zero length field:

    reallyZero REAL ::= 0

    09 00

All the rest values are encoded with length field greater than zero. The very first octet of contents then determines the way the real number is stored:

PLUS-INFINITY and MINUS-INFINITY are encoded by value of 40H and 41H respectively. Thus encoding of them is as follows:

All the standard numbers are then represented using one of two possible encodings - binary or character based representation.

Binary representation can use base of 2, 8 or 16. Format of contents is as follows:

1

S

B

B

F

F

0

0

E

E

E

E

E

E

E

E

1

S

B

B

F

F

0

1

E

E

E

E

E

E

E

E

E

E

E

E

E

E

E

E

1

S

B

B

F

F

1

0

E

E

E

E

E

E

E

E

E

E

E

E

E

E

E

E

E

E

E

E

E

E

E

E

octet #1

octet #2

octet #3

octet #4

1

S

B

B

F

F

1

1

0 < n < 256

E

E

E

E

E

E

E

E

...

E

E

E

E

E

E

E

E

octet #1

octet #2

octet #3

octet #(n+2)

The various fields in the first octet have following meanings:

S Sign
0 +1
1 -1
B B Base
0 0 2
0 1 8
1 0 16
1 1 reserved
F F Scaling factor
0 0 0
0 1 1
1 0 2
1 1 3

The overall value represented this way is S x N x 2F x BE where first three factors constitute the mantissa. The purpose of scaling factor is to avoid the encoder having to shift the mantissa so that its least significant bit is on an octet boundary. It is only needed with the base 8 and 16.

For example, Euler's number defined in ASN.1 can be encoded:

And the last possibility of real number representation is character-based. It is indicated by two MSB in first octet both being 0. With this representation, the second and all following octets contain essentially ASCII characters, forming a field as specified in ISO 6093. Which form of this ISO standard will be used is noted in lower bits of first octet.


NULL

The encoding of a NULL value is primitive: because there is only one value no contents octects are needed. Thus it is encoded as follows:


BOOLEAN

The encoding of boolean type is also primitive. The contents part consist of only one octet, which represents a value of FALSE if it is all zeroes, otherwise TRUE:

and there are 254 more alternatives for TRUE.


ENUMERATED

The encoding of an enumerated type is primitive and except the tag it is equal to encoding INTEGER value that represents it.