编程环境:C#Winform窗体应用程序服务器通过Socket向POS机通过无线信号发送数据包,由于POS机是单片机,所以接收到的中文字符是乱码,显示出来的结果全是问号“?”。但是POS机里面又无法设置UTF8,GB2312,UNICODE之类的编码。所以,要约定服务器和客户机都约定为一样的编码这条路走不通。请问还有什么方式可以解决乱码问题。望给出C#函数。谢谢!

解决方案 »

  1.   

    你必须 知道 POS机里面 默认的编码是什么,要不 怎么发送给他,如果不知道  你可以一个一个 试试UTF8,GB2312,UNICODE ==服务器和客户机都约定为一样的编码这条路你 必须走,没有 别的路 
      

  2.   

    这个问题你需要去找你的POS机而不是来找CSDN啊
    最简单的办法:找POS厂商,要他们提供往POS机写中文字符的SDK当然,你要是有时间有精力有实力,可以去拆了POS机,研究下它里面的字符是怎么存的,然后发送相应的编码过去
      

  3.   

    问题补充:
    我发送报文处的代码如下://编码可能为ASCII,UTF8,Default,GB2312,Unicode
                byte[] sendByteArray = Encoding.UTF8.GetBytes(strSendBack);            //发送数据回客户端POS机
                handler.BeginSend(sendByteArray, 0, sendByteArray.Length, 0, new AsyncCallback(SendCallBack), handler);
    用UTF8可以,但是POS机接收到的报文体除了中文以外的其他数据库发生了变化。得不到正常的数据了。请问这是什么原因造成的呢?
      

  4.   

    问题已经解决。
    POS机不认识UTF8或GB2312直接用Encoding.ASCII.GetBytes(str)方法得到的字节数组byte[],需要先将字符串转化为16进制字符串,然后将这个十六进制的字符串转化为字节数组,再发送给POS机,这样接收到的汉字就不是乱码了。
    现把我收集到的相关字符转化函数贴下,希望对将来需要的人有所帮助。        /// <summary>
            /// 从汉字转换到16进制
            /// </summary>
            /// <param name="s"></param>
            /// <returns></returns>
            public static string GetHexFromChs(string s)
            {
                if ((s.Length % 2) != 0)
                {
                    s += " ";//空格
                    //throw new ArgumentException("s is not valid chinese string!");
                }            System.Text.Encoding chs = System.Text.Encoding.GetEncoding("gb2312");            byte[] bytes = chs.GetBytes(s);            string str = "";            for (int i = 0; i < bytes.Length; i++)
                {
                    str += string.Format("{0:X}", bytes[i]);
                }            return str;
            }        /// <summary>
            /// 从汉字转换到16进制
            /// </summary>
            /// <param name="s"></param>
            /// <param name="charset">编码,如"utf-8","gb2312"</param>
            /// <param name="fenge">是否每字符用逗号分隔</param>
            /// <returns></returns>
            public string ToHex(string s, string charset, bool fenge)
            {
                if ((s.Length % 2) != 0)
                {
                    s += " ";//空格                
                }
                System.Text.Encoding chs = System.Text.Encoding.GetEncoding(charset);
                byte[] bytes = chs.GetBytes(s);
                string str = "";
                for (int i = 0; i < bytes.Length; i++)
                {
                    str += string.Format("{0:X}", bytes[i]);
                    if (fenge && (i != bytes.Length - 1))
                    {
                        str += string.Format("{0}", ",");
                    }
                }
                return str.ToLower();
            }        /// <summary>
            /// 从16进制转换成汉字
            /// </summary>
            /// <param name="hex"></param>
            /// <param name="charset">编码,如"utf-8","gb2312"</param>
            /// <returns></returns>
            public static string UnHex(string hex, string charset)
            {
                if (hex == null)
                    throw new ArgumentNullException("hex");
                hex = hex.Replace(",", "");
                hex = hex.Replace("\n", "");
                hex = hex.Replace("\\", "");
                hex = hex.Replace(" ", "");
                if (hex.Length % 2 != 0)
                {
                    hex += "20";//空格
                }
                // 需要将 hex 转换成 byte 数组。
                byte[] bytes = new byte[hex.Length / 2];            for (int i = 0; i < bytes.Length; i++)
                {
                    try
                    {
                        // 每两个字符是一个 byte。
                        bytes[i] = byte.Parse(hex.Substring(i * 2, 2),
                        System.Globalization.NumberStyles.HexNumber);
                    }
                    catch
                    {
                        // Rethrow an exception with custom message.
                        throw new ArgumentException("hex is not a valid hex number!", "hex");
                    }
                }
                System.Text.Encoding chs = System.Text.Encoding.GetEncoding(charset);
                return chs.GetString(bytes);
            }        /// <summary>
            /// 从16进制转换成汉字
            /// </summary>
            /// <param name="hex"></param>
            /// <returns></returns>
            public static string GetChsFromHex(string hex)
            {
                if (hex == null)
                    throw new ArgumentNullException("hex");
                if (hex.Length % 2 != 0)
                {
                    hex += "20";//空格
                    //throw new ArgumentException("hex is not a valid number!", "hex");
                }
                // 需要将 hex 转换成 byte 数组。
                byte[] bytes = new byte[hex.Length / 2];            for (int i = 0; i < bytes.Length; i++)
                {
                    try
                    {
                        // 每两个字符是一个 byte。
                        bytes[i] = byte.Parse(hex.Substring(i * 2, 2),
                            System.Globalization.NumberStyles.HexNumber);
                    }
                    catch
                    {
                        // Rethrow an exception with custom message.
                        throw new ArgumentException("hex is not a valid hex number!", "hex");
                    }
                }            // 获得 GB2312,Chinese Simplified。
                System.Text.Encoding chs = System.Text.Encoding.GetEncoding("gb2312");
                return chs.GetString(bytes);
            }        /// <summary>
            /// 字符串转16进制字节数组
            /// </summary>
            /// <param name="hexString"></param>
            /// <returns></returns>
            public byte[] strToToHexByte(string hexString)
            {
                hexString = hexString.Replace(" ", "");
                if ((hexString.Length % 2) != 0)
                    hexString += " ";
                byte[] returnBytes = new byte[hexString.Length / 2];
                for (int i = 0; i < returnBytes.Length; i++)
                    returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
                return returnBytes;
            }        /// <summary>
            /// 字节数组转16进制字符串
            /// </summary>
            /// <param name="bytes"></param>
            /// <returns></returns>
            public string byteToHexStr(byte[] bytes)
            {
                string returnStr = "";
                if (bytes != null)
                {
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        returnStr += bytes[i].ToString("X2");
                    }
                }
                return returnStr;
            }