34071417 发表于 2013-10-25 08:32:06

C# unicode编码和GB2312转换代码,之前一直纠结的编码转换

和上位机通讯最麻烦的就是编码转换,一般都是unicode和gb2312之间的转换。
在这里放出C#下的编码转换。
发个帖子,留个底,以后还能回头看看。
            mname 为gb2312字符数组的指针,mnamelength为字节长度
            // Create two different encodings.
            Encoding ascii = Encoding.ASCII;
            Encoding unicode = Encoding.Unicode;

            // Convert the string into a byte array.
            byte[] unicodeBytes = Encoding.Convert(Encoding.GetEncoding(936), unicode, mname,0,mnamelength);
这是转unicode的;

            // Create two different encodings.
            Encoding ascii = Encoding.ASCII;
            Encoding unicode = Encoding.Unicode;
            // Convert the string into a byte array.
            byte[] unicodeBytes = unicode.GetBytes(textBox_username.Text);
            // Perform the conversion from one encoding to the other.
            byte[] asciiBytes = Encoding.Convert(unicode, Encoding.GetEncoding(936), unicodeBytes);
这是转gb2312的;
之前在做液晶显示的,最后发现是unicode编码在液晶上显示乱码,还是需要转换成gb2312格式的。

wsh 发表于 2013-10-28 11:40:30

楼主你的意思是你用查表的方式来转换UNICODE编码吗???

wsh 发表于 2013-10-28 11:41:07

是不是要整个UNICODE表都做上去啊

34071417 发表于 2013-10-28 15:21:19

wsh 发表于 2013-10-28 11:41 static/image/common/back.gif
是不是要整个UNICODE表都做上去啊

不用做表,这只是在上位机端转换而已,要是在下位机做,就需要表了
页: [1]
查看完整版本: C# unicode编码和GB2312转换代码,之前一直纠结的编码转换