# dwg文件格式中MODULAR CHARS（模块字符）的详解（压缩算法）

Modular characters are a method of storing compressed integer values. They are used in the object map to

indicate both handle offsets and file location offsets. They consist of a stream  of bytes, terminating when

the high bit of the byte is 0.

In each byte, the high bit is a flag; when set, it indicates that  another byte follows. The concept is not

difficult to understand, but is a little difficult to explain. Lets look  at an example.

Assume the next two bytes in the file are:

10000010 00100100

We read bytes until we reach a byte with a high bit of 0.  Obviously the second byte meets that criterion.

Since we are reading from least significant to most significant, let's reverse the order of the bytes so that

they read MSB to LSB from left to right.

10000010 00100100

Now we drop the high order flag bits:

And then re-group the bits from right to left, padding on the left with 0's:

Result = 2 + 18*256 = 4610

Here’s another example using the basic formF1101001 F0010111 F1100110 00110101:

11101001 10010111 11100110 00110101

11101001 10010111 11100110 00110101

We read bytes until we reach a byte with a high bit of 0.  Obviously the fourth byte meets that criterion.

Since we are reading from least significant to most significant, let's reverse the order of the bytes so that

they read MSB to LSB from left to right.

Now we drop the high order flag bits:

And then re-group the bits from right to left, padding on the left with 0's:

Result:233+139*256+185*256^2+6*256^3=112823273

This process is further complicated by the fact that if the final byte (high bit 0) also  has the 64 bit (0x40)

set, this means to negate the number.

This is a negative number: 10000101 01001011

Since we are reading from least significant to most significant, let's reverse the order of the bytes so that

they read MSB to LSB from left to right.

We then clear the bit that was used to represent the negative number, and note that the result must be

negated:

Now we drop the high order flag bits:

And then re-group the bits from right to left, padding on the left with 0's:

Result: 133+5*256=1413, which we negate to get 1413

Modular chars are also used to store handle offsets in the object map. In this  case there is no negation

used; handles in the object map are always in increasing order.

MODULAR SHORTS

Modular shorts work just like modular chars -- except that the base module is a short  instead of a char.

There are only two cases to worry about here (from a practical point of view), because, in the case of

shorts, two modules make a long, and since these are used only to indicate object sizes, a  maximum

object size of 1 GB is probably correct.

00110001 11110100 10001101 00000000.

Reverse the order of the shorts:

00110001 11110100 10001101 00000000。

Reverse the order of the bytes in each short:

Drop the high order flag bit of each short:

And then re-group the bits from right to left, padding on the left with 0's:

Result: 62513+70*65536=4650033