[英]How to send string in utf-8 to irc server?
I have an irc bot written in c++ with the use of Qt library.我有一个使用 ZE8801102A40AD89DDCFDCAEBF008D25Z 库用 c++ 编写的 irc 机器人。 I store console text input in std::string, and then i'm using QSocket to post it on irc chat.
我将控制台文本输入存储在 std::string 中,然后我使用 QSocket 将其发布到 irc 聊天中。 But the problem is im want to use special signs (polish letters), which dont appear properly on chat.
但问题是我想使用特殊符号(抛光字母),这些符号在聊天中显示不正确。 What is the problem?
问题是什么? The way i use QSocketis:
我使用 QSocketis 的方式:
void Socket::poster(const QByteArray send)
{
mSocket->write(send);
mSocket->flush();
mSocket->reset();
}
QByteArray i create from std::string and std::cin我从 std::string 和 std::cin 创建的 QByteArray
he code's long so i only post the parts crucial for the specific functonality which fails他的代码很长,所以我只发布对失败的特定功能至关重要的部分
Socket class (which is the main class in the program, providing data to other classes): Socket class(程序中主要的class,为其他类提供数据):
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
protected:
QSslSocket *mSocket;
--------------------
connect(mSocket, SIGNAL(readyRead()),
this, SLOT(readyReady())
--------------------
//console input:
QThread *thread = new QThread;
consoleInput = new ConsoleInput();
consoleInput->startConsole(thread, mSocket);
consoleInput->moveToThread(thread);
thread->start();
-------------------
void Socket::readyReady()
{
QString data;
data2 = data;
mSocket->ReadOnly;
while(mSocket->canReadLine())
{
data = mSocket->readLine();
}
mSocket->reset();
}
---------------------
void Socket::poster(const QByteArray send) //sending to irc from many classes news, console itd
{
mSocket->write(send);
mSocket->flush();
mSocket->reset();
}
-------------------
ConsoleInput class (which takes console input, which is later sent to irc chat):
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void ConsoleInput::run()
{
std::cout << "!ConsoleInput::run()" << "\n";
while(1){
std::string input;
std::getline(std::cin, input);
determineOption(input);
if(input[0] != '/' || input[0] != '\\')
postInput(input);
input.clear();
}
}
----------------------------------
void ConsoleInput::postInput(std::string &input)
{
if(input[0]=='/')
return; //this prevents bot poting "/command" to channel
std::string lineToPost;
std::cout << "!lineToPost - input " << input << "\n";
ColourManipulation c;
lineToPost = "PRIVMSG #grunge " + c.addColours(input) + "\r\n";
emit mySignal(QByteArray::fromStdString(lineToPost)); // problem
}
Check the code can accept & show non-ascii characters:检查代码是否可以接受并显示非 ascii 字符:
std::string input;
std::getline(std::cin, input);
std::cout << input;
You need:你需要:
std::string &input
.std::string &input
。
std::string
type per se uses no encoding -- it will return the bytes you put in it - What encoding does std::string.c_str() use?std::string
类型本身不使用编码——它将返回你放入其中的字节——std::string.c_str() 使用什么编码? ..
Import the bytes into QString
using necessary encoding convertion使用必要的编码转换将字节导入
QString
Export the resulting QString
to UTF-8 encoded QByteArray
( QByteArray
itself is just an array of bytes too).将生成的
QString
导出到 UTF-8 编码的QByteArray
( QByteArray
本身也只是一个字节数组)。
Write the QByteArray
to a socket.将
QByteArray
写入套接字。
You can write something like the following:您可以编写如下内容:
/*
From doc: QTextCodec::codecForLocale()
Returns a pointer to the codec most suitable for this locale.
The codec will be retrieved from ICU where that backend is in use,
otherwise it may be obtained from an OS-specific API.
In the latter case, the codec's name may be "System".
*/
QTextCodec *codec = QTextCodec::codecForLocale(); // In most cases, it is not UTF-8
// Or set the encoding explicitly:
//QTextCodec *codec = QTextCodec::codecForName("Shift-JIS"); // put your input encoding here
QTextDecoder *decoder = codec->makeDecoder();
QByteArray chunk = QByteArray::fromStdString(input);
QString string = decoder->toUnicode(chunk);
delete decoder;
emit mySignal(string.toUtf8());
Be note that you can avoid std::string
and use QString
only:请注意,您可以避免
std::string
并仅使用QString
:
QString
is more comfortable to use, and, once received the data correctly, it always stores data in the same known format internally, despite of std::string
, which has no idea what data it stores. QString
使用起来更舒服,并且一旦正确接收到数据,它总是在内部以相同的已知格式存储数据,尽管std::string
不知道它存储什么数据。
How to read from console to QString
directly:如何直接从控制台读取到
QString
:
QTextStream in(stdin);
in.setCodec(<your console codec>);
QString input = in.readLine();
See QTextCodec and QTextStream .请参阅QTextCodec和QTextStream 。
Read also The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)另请阅读每个软件开发人员绝对、绝对必须了解 Unicode 和字符集的绝对最低要求(没有借口!)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.