串口、TCP、UDP传输中文字符时,先将字符串转内码。客户端接收到数据后,将内码转为字符串就OK了
QByteArray CommonFunction::strToInterCode(constQString &str)//字符串转内码
{
QString strName = str.toLocal8Bit();
QByteArray code;
code.append (strName);
while(code.count()<8)
{
code.append (QByteArray::fromHex ("20"));
}
qDebug ()<
return code;
}
//内码转字符串,要求为双字节数
QString CommonFunction::interCodeToStr(const QByteArray &data)
{
QString str;
QByteArray temp;
for(int i= 0;i
{
if(!(data[i] == 0x20 && data[i+1] == 0x20))
{
temp.append (data[i]);
temp.append (data[i+1]);
}
}
str = QString::fromLocal8Bit(temp);
qDebug ()<< "intercode