linked possible duplicate对我不起作用
我有一个10的字节数组和一个调试输出
“B \ 0 \ 0〜\ 0 \ 0S \ 0”
其中\ 0是控制字符(我认为)
并且字符串结果是
n
我有一些文字数据,128-129范围内有很多字符 特别是150(一个控制角色 - 开始保护区域)在一个我知道它们意味着em-dash的地方 几乎是正面的一些数据被读入win1252并被写为unicode
我尝试从UTF8获取一个字节数组,但它没有工作 我尝试从每个enconding中获取字节数组 在n和S之间是unicode decimal 150
下面有效,但我只进行了非常小的采样
Encoding win1252 = Encoding.GetEncoding("Windows-1252");
bool allgood = true;
List<byte> lByte = new List<byte>();
foreach (char c in @"n S".ToCharArray())
{
if ((Int16)c > 255)
{
Debug.WriteLine("problem");
allgood = false;
break;
}
else
lByte.Add((byte)c);
}
if (allgood)
{
s1252 = win1252.GetString(lByte.ToArray());
Debug.WriteLine(s1252);
}
从unicode转换为win1252的正确方法是什么?
这失败了
string inputStr = @"n S";
byte[] bytes = new byte[inputStr.Length * sizeof(char)];
System.Buffer.BlockCopy(inputStr.ToCharArray(), 0, bytes, 0, bytes.Length);
s1252 = win1252.GetString(bytes);
Debug.WriteLine(s1252);
有一个额外的字节194,结果是
n-S
这失败了
的Debug.WriteLine( “”);
unicodeBytes = unicode.GetBytes(@"n S");
foreach (byte b in unicodeBytes)
Debug.WriteLine(b.ToString() + " ub ");
// problem is here - get some good stuff but extra 0
win1252Bytes = Encoding.Convert(unicode, win1252, unicodeBytes);
char[] win1252Chars = new char[win1252.GetCharCount(win1252Bytes, 0, win1252Bytes.Length)];
Debug.WriteLine("");
foreach (char c in unicodeChars) //win1252Chars)
Debug.Write(c);
Debug.WriteLine(win1252Chars.ToString());
Debug.WriteLine("win1252Chars");