问题描述:
同一个汉字:“成”在下位机的编码是:B3C9,在C#里是:6210,(C#接受下位机编码)
请问,如果要将下位机发上的来的汉字编码变成C#里的UINCODE,并且把UINCODE转换成汉字并显示出来···
希望大家快来帮忙啊~~~~
同一个汉字:“成”在下位机的编码是:B3C9,在C#里是:6210,(C#接受下位机编码)
请问,如果要将下位机发上的来的汉字编码变成C#里的UINCODE,并且把UINCODE转换成汉字并显示出来···
希望大家快来帮忙啊~~~~
调试欢乐多
我给你写个方法吧,你传入的参数是什么?是2个byte(或者长度为2的byte[]),还是string("B2C9")?
char GetChar(byte,byte);
char GetChar(byte[]);
char GetChar(string);
如果是字节,你把第1个和第2个字节说清除(B3,C9的顺序)
你说了,我给你写,别忘了加分哦,呵呵
{
return char.Parse(Encoding.Default.GetString(bs));
}
其中Encoding.Convert方法可以满足你的需求。
using System;
using System.Text;
namespace ConvertExample
{
class ConvertExampleClass
{
static void Main()
{
string unicodeString = "This string contains the unicode character Pi(\u03a0)";
// Create two different encodings.
Encoding ascii = Encoding.ASCII;
Encoding unicode = Encoding.Unicode;
// Convert the string into a byte[].
byte[] unicodeBytes = unicode.GetBytes(unicodeString);
// Perform the conversion from one encoding to the other.
byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);
// Convert the new byte[] into a char[] and then into a string.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
string asciiString = new string(asciiChars);
// Display the strings created before and after the conversion.
Console.WriteLine("Original string: {0}", unicodeString);
Console.WriteLine("Ascii converted string: {0}", asciiString);
}
}
}
详见MSDN:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfSystemTextEncodingClassConvertic.htm
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfSystemTextEncodingMembersic.htm
是这样的,我上位机收到的是char类型的数组,“成”的字节顺序是:B3、C9,这样应该怎么做啊?
谢谢,昨天晚上网络不通,所以就没有及时看到信息。
你收到的就是{'成','的','哦','是'}这些字符?
你怎么知道B3、C9?B3,C9不是char啊,是byte啊
"B3C923A2" 这样的
还是
"成里网"
这样的?我怎么感觉我回答你的问题是义务呢?
第1.我回答了,你半天不回复
第2.我叫你把问题说清楚,你就是舍不得多打几个字到底是我求你,还是你求我?
真的不好意思:
string里面是这样:"C3B923A11234"
我再把这些放入了,Char弄的数组的:char []M={C,3,B,9```}这样
要把Char 数组的转成汉字