How can I get a string from byte () without changing its binary contents?

I have a language that usually contains serialized data messages in a human-readable format, but some derivatives in the language contain verbatim raw binary data.

My analyzer uses String for its buffer, as this is probably the easiest job. However, data is read from the network socket into the Byte array.

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 ASCII encoding would just leave my bytes would be invalid; any bytes with a value that does not fit into the 7-bit model have been translated to '?' .

So, I thought about using a single-byte encoding "Unicode", which should leave only my bytes, and also allow values ​​in 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 distorted. In fact, I could not determine exactly how the data is distorted, but I know that the length of the data changes, indicating that the bytes do not remain verbatim.

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

+4
source share
1 answer

Based on our discussion of comments, it seems like you want to see Byte instances in the absence of encoding. If so, you should use List(Of Byte) instead of String

+2
source

Source: https://habr.com/ru/post/1401418/


All Articles