编程环境:C#Winform窗体应用程序服务器通过Socket向POS机通过无线信号发送数据包,由于POS机是单片机,所以接收到的中文字符是乱码,显示出来的结果全是问号“?”。但是POS机里面又无法设置UTF8,GB2312,UNICODE之类的编码。所以,要约定服务器和客户机都约定为一样的编码这条路走不通。请问还有什么方式可以解决乱码问题。望给出C#函数。谢谢!
解决方案 »
- C# 急
- 蚁群算法
- 万事具备,只欠东风!
- C#的winform程序运行报表怎么总提示access数据库登陆的对话框呢?在线
- 给介绍一下怎么从零开始学习C#?新手,什么都不知道,怎么学习。NET?
- 为什么我用text控件把数据存到SQL2000的表中,表的内容会多出一个空格来!~
- C#.Net System.Security.Cryptography 下DES的加密 与 解密问题
- C#接口的功力从何而来?
- 编程中很实用的问题,有外键的DATAGRIDVIEW添加,更新,删除?
- VS2010 使用TFS,能下载到项目,但是始终是脱机状态,一联机VS就卡死自动关闭
- 判断特定的字符
- =========(分页)的制作=+求大家帮我下+====
最简单的办法:找POS厂商,要他们提供往POS机写中文字符的SDK当然,你要是有时间有精力有实力,可以去拆了POS机,研究下它里面的字符是怎么存的,然后发送相应的编码过去
我发送报文处的代码如下://编码可能为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机接收到的报文体除了中文以外的其他数据库发生了变化。得不到正常的数据了。请问这是什么原因造成的呢?
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;
}