Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385325

How can I obtain a String from a Byte() without mangling its binary contents?

I have a language that generally contains serialised data messages in a human-readable format, but some productions within the language contain verbatim raw, binary data.

My parser uses String for its buffer since that seems to be the easiest thing to work with. However the data is read from a network socket into an array of Byte.

Now, I'm trying to connect the dots between Byte() and String:

' data as Byte()
' count as Integer
' buffer as String

buffer += System.Text.Encoding.ASCII.GetString(data, 0, count)

But my initial assumption that an ASCII encoding would just leave my bytes alone turned out to be invalid; any bytes with a value that doesn't fit into the 7-bit model was translated into '?'.

So then I thought about using a single-byte "Unicode" encoding that should leave my bytes alone but also allow values throughout the 8-bit range:

' data as Byte()
' count as Integer
' buffer as String

Dim enc = New System.Text.UTF8Encoding
buffer += enc.GetString(data, 0, count)

But my data is still mangled. I haven't actually been able to deduce yet precisely how the data is being mangled, but I do know that the length of the data is changing, indicating that the bytes are not being left verbatim.

So how can I obtain a String whose contents are just a verbatim copy of the bytes from my Bytes() input?

Upvotes: 4

Views: 434

Answers (1)

JaredPar
JaredPar

Reputation: 755327

Based on our comment discussion it seems like you want to see the Byte instances in the abscence of an encoding. If this is the case you should consider using List(Of Byte) instead of String

Upvotes: 2

Related Questions