Character Encoding

christuckeruf

New member
Joined
May 7, 2011
Messages
3
Programming Experience
5-10
Hello

I am reading a byte stream from the network in UTF16 LE encoding. The byte stream has two bytes for each letter and looks like this:

65 00 66 00 67 00 68 00 ...

ABCD ...

The whole byte array stream is broken up into three distinct parts separated by a space:

65 00 66 00 67 00 32 00 65 00 66 00 67 00 32 00 65 00 66 00 67 00

ABC<SPACE>ABC<SPACE>ABC

I have two issues.

First, in the last example if I grabbed the ABC before the space (65 00 66 00 67 00) and use Unicode.GetString(bytearray) it comes across as:

ABC[] (where the last NULL character shows up)

instead of:

ABC

How do ensure that the encoding function reads the last NULL byte as part of the preceding character.

Secondly, in order to search the array for space characters I create a byte array for the space:

VB.NET:
Dim bvbSpace() As Byte = Unicode.GetBytes(" ")

However, it only returns a single byte (32) instead of the expected two (32 00). How can I get it to show two (Without manually adding a "00" to the array)

Thanks,

Chris
 
Dim bvbSpace() As Byte = Unicode.GetBytes(" ")

However, it only returns a single byte (32) instead of the expected two (32 00).
No, it returns a two byte array (32,0).

One way to do this could be use a MemoryStream and a StreamReader, ReadToEnd method will accurately read the valid chars available. With Unicode.GetByteCount you can see how many bytes is read so far.
 
Back
Top