Take the 2-minute tour ×

I have a byte array that's been initialized with 0xFF in each byte:

    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?

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

I would suggest just finding out how long the string will really be:

int firstFF = Array.IndexOf(buffer, (byte) 0xff, 192);
if (firstFF == -1)
    firstFF = buffer.Length;
stringToRead = Encoding.ASCII(buffer, 192, firstFF - 192);

I would not try to give Encoding.ASCII bytes 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:

byte[] data = { 0x41, 0x42, 0x43, 0xff, 0xff };
string text = Encoding.ASCII.GetString(data);
Console.WriteLine(text.Contains((char) 0xff)); // False
Console.WriteLine(text.TrimEnd((char) 0xff).Length); // Still 5...

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.

share|improve this answer
While I'd generally agree with you, there are cases in dealing with serial data where doing this kind of seek is a reasonable approach, since it is not known how many bytes have been read into the buffer. –  codekaizen Jan 31 '12 at 20:41
@codekaizen: Where did I say seeking is unreasonable? I said not to do it after decoding from binary to text, as it won't do what you want... –  Jon Skeet Jan 31 '12 at 20:47
Thanks Jon (and everyone else). This solution works very well for me! –  user685869 Jan 31 '12 at 21:04
add comment
var s = "Whatever" + new String((Char)0xFF, 32);
var trimmed = s.TrimEnd((Char)0xFF);

Alternatively, you can scan the string for the first index of the character, then take the substring:

var index = s.IndexOf((Char)0xFF);
var trimmed = s.Substring(0, index);
share|improve this answer
not sure, but this might fail because the 0xFF has been ASCII and is now encoded as UTF-16. –  Yahia Jan 31 '12 at 20:31
It works for me. It should work since the upper byte is 0. –  codekaizen Jan 31 '12 at 20:38
+1 as I said I was not sure... –  Yahia Jan 31 '12 at 20:43
This won't work if the byte array contains a bunch of invalid ASCII characters - because they won't end up as Unicode U+00FF, they'll end up as '?'. Just because it works with a hand-crafted string doesn't mean that it will work with the original byte array. –  Jon Skeet Jan 31 '12 at 20:45
(See my answer for an example of it not working.) –  Jon Skeet Jan 31 '12 at 20:47
add comment

Your Answer


