求助16进制的问题
我是菜鸟,请教各位,我想将一串16进制表达的字符转为文本,结果再用16进制编辑器打开时老是多出一些字符出来,比如下面的代码(改自MSDN):string hexValues = "44 41 45 48 80 44 44 44";
string[] hexValuesSplit = hexValues.Split(' ');
string yy = "";
foreach (String hex in hexValuesSplit)
{
int value = Convert.ToInt32(hex, 16);
string stringValue = Char.ConvertFromUtf32(value);
yy += stringValue;
}
StreamWriter sw = new StreamWriter("c:\\tmp.txt");
sw.Write(yy);
sw.Close();
结果用WinHex打开,显示为:“44 41 45 48 C2 80 44 44 44”,也就是说多了一个C2。估计是跟编码有关,但我一点都不懂,怎么能一一对应回去,请指教,谢谢! --------------------编程问答-------------------- 先转化为字符(用gb2312如果是中文的话) --------------------编程问答-------------------- 你得先确定你的源编码是不是UTF32...多字节编码也不是你这样转的,用System.Text.Encoding类...
如果只是存文件直接存byte,不要多此一举转来转去的... --------------------编程问答-------------------- FileStream fs = new FileStream("c:\\tmp.txt", FileMode.OpenOrCreate, FileAccess.Write);
BinaryWriter rw = new BinaryWriter(fs, Encoding.ASCII);
string hexValues = "44 41 45 48 80 44 44 44";
string[] hexValuesSplit = hexValues.Split(' ');
string yy = "";
foreach (String hex in hexValuesSplit)
{
int value = Convert.ToInt32(hex, 16);
char charValue = (char)value;
rw.Write(charValue);
}
rw.Close();
fs.Close(); --------------------编程问答-------------------- FileStream fs = new FileStream("c:\\tmp.txt", FileMode.OpenOrCreate, FileAccess.Write);
BinaryWriter rw = new BinaryWriter(fs);
string hexValues = "44 41 45 48 80 44 44 44";
string[] hexValuesSplit = hexValues.Split(' ');
string yy = "";
foreach (String hex in hexValuesSplit)
{
Byte value = Convert.ToByte(hex, 16);
//char charValue = (char)value;
//Console.WriteLine((short)value);
rw.Write(value);
}
rw.Close();
fs.Close(); --------------------编程问答--------------------
这个搞定了!高手!!也谢谢楼上各位! --------------------编程问答--------------------
补充:.NET技术 , C#