I have a byte array that’s been initialized with 0xFF in each byte:
for (int i = 0; i < buffer.Length; i++)
{
buffer[i] = 0xFF;
}
Once this byte array has been filled with valid data, I need to extract an ASCII string that’s stored at offset 192 and may be up to 32 characters in length. I’m doing this like so:
ASCIIEncoding enc = new ASCIIEncoding();
stringToRead = enc.GetString(buffer, 192, 32);
This works but I need to strip off the trailing bytes that contain 0xFF to avoid the string looking something like “John Smith??????????????????????”. Is there a function in .NET that provides this ability? Something like the String.TrimEnd() function perhaps or am I looking at a regex to do this?
I would suggest just finding out how long the string will really be:
I would not try to give
Encoding.ASCIIbytes which aren’t valid ASCII-encoded text. I don’t know offhand what it would do with them – I suspect it would convert them to?to show the error (as suggested by your existing output), but then you wouldn’t be able to tell the difference between that and real question marks. For example:Now you could create an encoding which used some non-ASCII replacement character… but that’s a lot of hassle when you can just find where the binary data stops being valid.