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?
Stringfrom aByte()whilst maintaining this encoding-agnosticism. Perhaps VB.NET doesn't support this?byte()?Stringto stop caring about encoding and be a nice automatically-resizing array of bytes for me.List(Of Byte). It's the rough equivalent ofstd::vector<byte>and probably closer to what you're looking for.