开发者

C#- How to handle string terminator in the conversion

I have a binary array. In the process of conversion it into string,due to some data my string gets terminated. and it ignores next data.Have a look on my code. Is there is any mistake??

str += (char)chunkData[index].ToString(); 

Later on i want to display it on textbox. My array contains following data as display in hex editor.

 xÚb```e``*bxÚb`
    ¨€ˆY8ÄX¡˜A‰“yuZs˜#µjтЬi@š
    È4„è0

I tried this but nothing change.开发者_运维技巧. str = System.Text.Encoding.ASCII.GetString(chunkData);


You shouldn't use text to represent arbitrary binary data. You will almost certainly lose data if you just use Encoding.GetString and Encoding.GetBytes.

If you really want to convert arbitrary binary data to text and back, use Convert.ToBase64String and Convert.FromBase64String.

As for '\0' termination: .NET strings themselves don't rely on termination characters, but many UI controls (including TextBox) will treat '\0' as a termination character.


See Encoding.GetString with the encoding matching your binary data.

str = System.Text.Encoding.ASCII.GetString(chunkData);


Your "mistake" is not using the Encoding.GetString(byte[]) call for that :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜