Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the character that represents a null byte after ASCIIEncoding.GetString(byte[])?

I have a byte array that may or may not have null bytes at the end of it. After converting it to a string I have a bunch of blank space at the end. I tried using Trim() to get rid of it, but it doesn't work. How can I remove all the blank space at the end of the string after converting the byte array?

I am writing this is C#.

like image 361
Brian Avatar asked Jan 22 '23 10:01

Brian


1 Answers

Trim() does not work in your case, because it only removes spaces, tabs and newlines AFAIK. It does not remove the '\0' character. You could also use something like this:

byte[] bts = ...;

string result = Encoding.ASCII.GetString(bts).TrimEnd('\0');

like image 159
anakic Avatar answered Feb 08 '23 13:02

anakic