摆脱字符串中的扩展ASCII字符(Getting rid of Extended ASCII characters in a string)

我有一个扩展名为ASCII char的字符串,我试图将其删除。 如何在字符串中找到它并从字符串中删除它: 1ÿ1ÿ0ÿÿÿ ?

字节数组,buffer = {49,0,255,255,49,0,255,255,48,0,255,255,255,255}

我正在使用C#,字符串是由像这样的字节数组形成的: temp.Add(System.Text.Encoding.ASCII.GetString(buffer));

然后,temp中的第一项是"1\0??1\0??0\0????"

我想从字符串中删除非ASCII值,或者更好的是缓冲区。

I have a string that has extended ASCII char ÿ in it and I am trying to delete it. How do I find it in a string and delete it from a string like this: 1ÿ1ÿ0ÿÿÿ?

The byte array, buffer = { 49, 0, 255, 255, 49, 0, 255, 255, 48, 0, 255, 255, 255, 255 }

I am using C# and the string was formed from a byte array like so: temp.Add(System.Text.Encoding.ASCII.GetString(buffer));

Then, the first item in temp is "1\0??1\0??0\0????"

I would like to remove the non-ASCII values from the string, or better yet the buffer.

最满意答案

要在创建字符串之前从缓冲区中删除字符:

byte[] buffer = new byte[] { 49, 0, 255, 255, 49, 0, 255, 255, 48, 0, 255, 255, 255, 255 } var cleanBuffer = buffer.Where((b) => b < 128).ToArray(); string temp = Encoding.ASCII.GetString(cleanBuffer);

如果你试图将它转换为字符串,然后删除有问题的字符,你不能说出合法的区别? 因为转换失败而放置在那里的角色和角色。 也就是说,如果你的缓冲区包含:

{ 63, 63, 49, 0, 255, 255, 49, 0, 255, 255, 48, 0, 255, 255, 255, 255 }

然后生成的字符串将以??1\0??开头。 。 前两个问号是合法的,但最后两个是转换失败的结果。

To remove the characters from the buffer before creating the string:

byte[] buffer = new byte[] { 49, 0, 255, 255, 49, 0, 255, 255, 48, 0, 255, 255, 255, 255 } var cleanBuffer = buffer.Where((b) => b < 128).ToArray(); string temp = Encoding.ASCII.GetString(cleanBuffer);

If you try to convert it to a string and then remove the offending characters, you can't tell the difference between a legitimate ? character and one that was placed there because conversion failed. That is, if your buffer contained:

{ 63, 63, 49, 0, 255, 255, 49, 0, 255, 255, 48, 0, 255, 255, 255, 255 }

Then the resulting string would start with ??1\0??. The first two question marks are legitimate, but the last two are the result of conversion failure.

摆脱字符串中的扩展ASCII字符(Getting rid of Extended ASCII characters in a string)

我有一个扩展名为ASCII char的字符串,我试图将其删除。 如何在字符串中找到它并从字符串中删除它: 1ÿ1ÿ0ÿÿÿ ?

字节数组,buffer = {49,0,255,255,49,0,255,255,48,0,255,255,255,255}

我正在使用C#,字符串是由像这样的字节数组形成的: temp.Add(System.Text.Encoding.ASCII.GetString(buffer));

然后,temp中的第一项是"1\0??1\0??0\0????"

我想从字符串中删除非ASCII值,或者更好的是缓冲区。

I have a string that has extended ASCII char ÿ in it and I am trying to delete it. How do I find it in a string and delete it from a string like this: 1ÿ1ÿ0ÿÿÿ?

The byte array, buffer = { 49, 0, 255, 255, 49, 0, 255, 255, 48, 0, 255, 255, 255, 255 }

I am using C# and the string was formed from a byte array like so: temp.Add(System.Text.Encoding.ASCII.GetString(buffer));

Then, the first item in temp is "1\0??1\0??0\0????"

I would like to remove the non-ASCII values from the string, or better yet the buffer.

最满意答案

要在创建字符串之前从缓冲区中删除字符:

byte[] buffer = new byte[] { 49, 0, 255, 255, 49, 0, 255, 255, 48, 0, 255, 255, 255, 255 } var cleanBuffer = buffer.Where((b) => b < 128).ToArray(); string temp = Encoding.ASCII.GetString(cleanBuffer);

如果你试图将它转换为字符串,然后删除有问题的字符,你不能说出合法的区别? 因为转换失败而放置在那里的角色和角色。 也就是说,如果你的缓冲区包含:

{ 63, 63, 49, 0, 255, 255, 49, 0, 255, 255, 48, 0, 255, 255, 255, 255 }

然后生成的字符串将以??1\0??开头。 。 前两个问号是合法的,但最后两个是转换失败的结果。

To remove the characters from the buffer before creating the string:

byte[] buffer = new byte[] { 49, 0, 255, 255, 49, 0, 255, 255, 48, 0, 255, 255, 255, 255 } var cleanBuffer = buffer.Where((b) => b < 128).ToArray(); string temp = Encoding.ASCII.GetString(cleanBuffer);

If you try to convert it to a string and then remove the offending characters, you can't tell the difference between a legitimate ? character and one that was placed there because conversion failed. That is, if your buffer contained:

{ 63, 63, 49, 0, 255, 255, 49, 0, 255, 255, 48, 0, 255, 255, 255, 255 }

Then the resulting string would start with ??1\0??. The first two question marks are legitimate, but the last two are the result of conversion failure.