查看完整版本: [-- 关于Qtcpsocket中文发送和处理的问题. --]

QTCN开发网 -> Qt中文处理 -> 关于Qtcpsocket中文发送和处理的问题. [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

songzhao831 2014-05-01 00:55

关于Qtcpsocket中文发送和处理的问题.

平台是ubuntu+QT5.2.1
发现发送中文到服务器, 服务是JSP的. 定为GB2312编码. 已经用了QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB18030-0"));

但服务器收到的还是乱码. 目前代码是这样写的.  大家帮我看看怎么改能正常发中文.

if(this->send_message.isNull())
        {
            std::string str = this->message.toStdString();
            const char* ch = str.c_str();
            tcpSocket->write(ch);//buffer is null, fill it;

toby520 2014-05-01 11:40
对于Qt5.x它默认编码都是utf-8的你Qt里面设置编码应该没有用了吧

songzhao831 2014-05-01 17:48
可是怎么把GB2312的编码的字符串发出去呢???

dbzhang800 2014-05-01 20:45
songzhao831:可是怎么把GB2312的编码的字符串发出去呢??? (2014-05-01 17:48) 

使用合适的QTextCodec进行转换啊,

QByteArray QTextCodec::fromUnicode(const QString & str) const

songzhao831 2014-05-03 21:12
dbzhang800:使用合适的QTextCodec进行转换啊,
QByteArray QTextCodec::fromUnicode(const QString & str) const
 (2014-05-01 20:45) 

这函数怎么老是调不了啊.

./../express/main.cpp: In function 'int main(int, char**)':
../../express/main.cpp:13: error: cannot call member function 'QByteArray QTextCodec::fromUnicode(const QString&) const' without object
make: *** [main.o] Error 1
21:06:19: The process "/usr/bin/make" exited with code 2.

songzhao831 2014-05-04 00:05
现在遇到两个编码问题: 读ini文件 和发送数据.


    QByteArray test = ConfigIni_read->value("/basicinfo/v_dz").toByteArray();    //address
  // stationInfo.v_dz= ConfigIni_read->value("/basicinfo/v_dz").toString() + "#|";
    QTextCodec *codec=QTextCodec::codecForName("UTF-8"); //设定字符集为GBK(大小写均可,还可用GB18030代替)
    QString outStr = codec->toUnicode(test);
打印出乱码,


   QTextCodec *codec = QTextCodec::codecForName("UTF-8");
       //  temp = codec->toUnicode(send_message);
        codec->fromUnicode(send_message);
        tcpSocket->write(codec->fromUnicode(send_message));/

发送出去还是不对, 服务不能得到正确的编码.

dbzhang800 2014-05-04 16:08
你需要确定,你遇到的是不是编码问题。

比如,对于tcp来说,

首先,你发送接收ASCII字符串是正常的么?

其次,你可以随便找一个QString字符串,转成utf8的QByteArray字节流,再转回QString字符串,是正常的么?


songzhao831 2014-05-11 00:24
dbzhang800:你需要确定,你遇到的是不是编码问题。
比如,对于tcp来说,
首先,你发送接收ASCII字符串是正常的么?
....... (2014-05-04 16:08) 

我得到的字符串没有问题, 中文经转换后均能正常显示. 但我发的这个服务器那边就是乱码.
发出时, 转之前这个打印就是乱码, 转之后打出来的是正常显示的.  收到的也是转过后, 能正常显示.

onsocketconnected: "HEAD=000096#|6000#|40#|0001#|22000000002#|BODYDATA=99988#|��DEWQ#|E1#|20#|10014001#|1#|2014-04-25#|U#|"
"HEAD=000096#|6000#|40#|0001#|22000000002#|BODYDATA=99988#|北DEWQ#|E1#|20#|10014001#|1#|2014-04-25#|U#|"
read file: 0
tcpConnection::readMessage "HEAD=000064#|0001#|22000000002#|                   #|更新标志报文格式非法#|"
tcpConnection::displayError: "The remote host closed the connection"

songzhao831 2014-05-11 00:25
songzhao831:我得到的字符串没有问题, 中文经转换后均能正常显示. 但我发的这个服务器那边就是乱码.
发出时, 转之前这个打印就是乱码, 转之后打出来的是正常显示的.  收到的也是转过后, 能正常显示.
onsocketconnected: "HEAD=000096#|6000#|40#|0001#|22000000002#|BODYDATA .. (2014-05-11 00:24) 

如果没有中文, 用英文和数字, 一切正常. 只要中间写的有中文就出问题. 很郁闷啊.
QT5.2.1 linux上的.

songzhao831 2014-05-20 10:27
已经搞定. 谢谢版主. c++文件编码没设好.


查看完整版本: [-- 关于Qtcpsocket中文发送和处理的问题. --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled