K
Klaus Petersen
Hi.
I'm trying to figure out how the BinaryWriter class stores strings - the
BinaryWriter is attached to a MemoryStream.
BinaryWriter inserts stores a value just before the actual string to reveal
the length of the string to follow.
If the length of the string is less than 128 chars, its length is stored as
a single byte.
If the string is longer than 127 chars, the length is stored as 2 bytes -
and the length can be restored from the 2 bytes in the following way:
length = byte [1] * 128 + byte [0] - 128
This way of storing the length should be good enough to enable any contence
in the string (e.g. chars from 0 to 255).
However, BinaryWriter still converts all chars of value 128 into 63, which
makes a reader unable to tell them apart.
If you force a char in the string to be of value 128, the BinaryReader
converts chars of value 128 to 172, which makes the code using the output of
the BinaryReader unable to tell these two chars apart.
I'm using default encoding on the BinaryReader and the BinaryWriter aswell.
Can someone explain why the BinaryReader/BinaryWriter has this "feature" or
suggest a solution?
Regards
Klaus
I'm trying to figure out how the BinaryWriter class stores strings - the
BinaryWriter is attached to a MemoryStream.
BinaryWriter inserts stores a value just before the actual string to reveal
the length of the string to follow.
If the length of the string is less than 128 chars, its length is stored as
a single byte.
If the string is longer than 127 chars, the length is stored as 2 bytes -
and the length can be restored from the 2 bytes in the following way:
length = byte [1] * 128 + byte [0] - 128
This way of storing the length should be good enough to enable any contence
in the string (e.g. chars from 0 to 255).
However, BinaryWriter still converts all chars of value 128 into 63, which
makes a reader unable to tell them apart.
If you force a char in the string to be of value 128, the BinaryReader
converts chars of value 128 to 172, which makes the code using the output of
the BinaryReader unable to tell these two chars apart.
I'm using default encoding on the BinaryReader and the BinaryWriter aswell.
Can someone explain why the BinaryReader/BinaryWriter has this "feature" or
suggest a solution?
Regards
Klaus