2

Suppose I have a Byte array and I use Encoding.ASCII.GetString() to convert the bytes into a string. Now the first few bytes will contain actual values, but the remaining ones will all have value of 0. After obtaining the string, when I show it on a WPF Form, there are some non printable characters. How can I remove these non printable characters. One way would be to loop through the array and only consider up to an index not containing 0, but I may also Encode using Unicode, that is Encoding.Unicode.GetString().

What would be the most generic way to solve the problem.

Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
  • Do you know the actual encoding of the Byte array? I'm imagining the input array to be Chinese, UTF8 encoding, for example. – tofutim Jun 02 '11 at 08:05
  • @tofutim: Yes, I know the actual encoding that is being used, but as I mentioned, it will vary from scenario to scenario. – Shamim Hafiz - MSFT Jun 02 '11 at 08:06
  • 1
    Are you insuring for when the encoding is unknown and you choose ASCII? Converting from a foreign language could lead to diamonds and hearts, etc. Do you just mean to strip those weird ASCII non-characters like beeps? How about linefeeds and tabs? – tofutim Jun 02 '11 at 08:08
  • @tofutim: There are no line feeds and tabs. It will be some characters and the remaining of the byte array will be zero. Had it been just ASCII Encoding, I would just look for first zero. – Shamim Hafiz - MSFT Jun 02 '11 at 08:11

2 Answers2

4
var buffer = new byte[] { 65, 66, 67, 68, 0, 0, 0, 0, 0 };
var length = buffer.TakeWhile(b => b != 0).Count();
var text = Encoding.UTF8.GetString(buffer, 0, length);
Anthony Faull
  • 17,549
  • 5
  • 55
  • 73
0

I'd just find the first zero in the array and strip off everything that follows.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490