BinaryWriter: WriteValue |
Writes the binary representation of a datatype to the underlying stream.
Public Sub WriteValue( ByRef Value As Variant, Optional ByRef Index As Variant, Optional ByRef Count As Variant )
Index and vCount are only valid when writing Byte arrays, Integer array, and Strings. For all other types, these parameters are ignored.
Simple datatypes such as vbLong and vbDouble are written out exactly as they appear in memory. Each individual byte of that datatype is written. For example, a vbLong is 4 bytes in memory, even if its value is 0. The 4 bytes are written to the stream, not one byte with a value of 0. A vbDouble is 8 bytes, so all 8 bytes are written as is to the stream.
The one exception is when writing a String value. Both the number of bytes from an encoded string and the encoded bytes are written, however, the length is not written as the individual bytes, like the other datatypes. The length of the string is written as a series of 7bit values. Each byte for the length is calculated by starting out with the actual value, then ANDing it with &H7F to take the first 7 bits. The value is ORed with &H80 to signify there are more bytes to follow for the length. Once that byte is written, the original value is shifted right 7 bits and the process starts over until the value reaches less than &H80. The final byte value is not ORed with &H80, it is written as is without the high bit set. When reading the bytes back, the byte without the high bit set will signal as the last byte in the value.
Do While value > &H80 WriteValue CByte((value And &H7F) Or &H80) value = (value And &HFFFFFF80) \ &H80 Loop WriteValue CByte(value)
Byte arrays are written as is. There is no description written along with the byte values. The number of bytes will have to be known in advance or manually stored in the stream.
Integer arrays are treated as characters and are converted to the encoded bytes using the current encoding. Like the byte array, the number of bytes is not stored with the encoded characters and will have to be manually stored.
The Decimal datatype is converted to 16 bytes with the same
layout as used in .NET. The layout is different than that of VB.
VB Decimal layout
bytes 0-1: Variant datatype information
bytes 2: precision
bytes 3: sign (&H80 is negative)
bytes 4-7: the 32 highest bits in the 96bit value
bytes 8-11: the 32 lowest bits in the 96bit value
bytes 12-15: the 32 middle bits in the 96bit value
.NET Decimal layout
bytes 0-3: the 32 lowest bits in the 96bit value
bytes 4-7: the 32 middle bits in the 96bit value
bytes 8-11: the 32 highest bits in the 96bit value
bytes 12-13: unused (zero)
bytes 14: precision
bytes 15: sign (&H80 is negative)