I'm trying to send encoding strings over the socket. The data that is received from a line edit. Then parsed then sent to the socket. When parsing the string, I'm checking to make sure that the information doesn't go over 510 bytes to the server. So I use QTextCodec::fromUnicode() on each character in the QString and check the length of that character and add it and if it goes over the limit, I split it until it's not possible to split anymore.

Now here is the issue. When I sent a short japanese text string (for testing), I test in Konversation and Quassel. It comes out perfectly. But if I send where it's alot it comes out with strange obfuscated text "€ãƒ¦ãƒ¼ã‚ ¶Ã£Æ’Â¼Ã£Æ’Â»Ã£â€šÂµÃ£Æ ’ã". Instead of what I'm sending. All the IRC clients including mine are using UTF-8.

Here is the code for the string parser.
Qt Code:
  1. void parsePrivmsg(const QString &message)
  2. {
  3. QString msg = message;
  4.  
  5. if (msg.isEmpty()) {
  6. window->socket()->rfcPrivmsg(QString(), msg);
  7. return;
  8. }
  9.  
  10. QString dest = msg.left(msg.indexOf(QChar(' ')));
  11. msg.remove(0, dest.length() + 1);
  12.  
  13. if (!msg.isEmpty()) {
  14. if (dest.toLower() == window->name()) {
  15. if (window->windowType() == Aki::BaseWindow::ChannelWindow) {
  16. Aki::ChannelWindow *channel = qobject_cast<Aki::ChannelWindow*>(window);
  17. foreach (Aki::Irc::User *user, channel->users()) {
  18. if (user->nick() == channel->socket()->currentNick()) {
  19. QString nickColour = QString("<font color='%1'>%2</font>")
  20. .arg(user->color().name(), user->nick());
  21. QStringList messages = checkMessageLength("PRIVMSG", dest, msg, user);
  22.  
  23. QStringListIterator iter(messages);
  24. while (iter.hasNext()) {
  25. QString tmp = iter.next();
  26. channel->socket()->rfcPrivmsg(dest, tmp);
  27. channel->view()->addPrivmsg(nickColour, Aki::Irc::Color::toHtml(tmp));
  28. }
  29. }
  30. }
  31. } else if (window->windowType() == Aki::BaseWindow::QueryWindow) {
  32. }
  33. }
  34. }
  35. }
  36.  
  37. QStringList checkMessageLength(const QString &type, const QString &destination,
  38. const QString &message, Aki::Irc::User *user)
  39. {
  40. const int nickLength = user->nick().length();
  41. const int hostMaskLength = user->hostMask().length();
  42. const int maxLength = 512 - 8 - (nickLength + hostMaskLength + type.length() +
  43. destination.length());
  44. int lastSplitLength;
  45. int charLength;
  46. int lastBreakPosition;
  47.  
  48. lastSplitLength = charLength = lastBreakPosition = 0;
  49. QString process = message;
  50. QStringList split;
  51.  
  52. QTextCodec *codec = window->socket()->codec();
  53.  
  54. int index = 0;
  55.  
  56. // Continue looping while the message still needs to be
  57. // broken down
  58. while (process.length() >= maxLength) {
  59. // We need to check each character to get the proper size of character.
  60. QByteArray ch = codec->fromUnicode(QString(process[index]));
  61. charLength = ch.length();
  62. lastSplitLength += charLength;
  63.  
  64. if (process[index].isSpace() || process[index].isPunct()) {
  65. lastBreakPosition = index;
  66. }
  67.  
  68. if (charLength + lastSplitLength >= maxLength) {
  69. if (lastBreakPosition != 0) {
  70. split << process.left(lastBreakPosition);
  71. process.remove(0, lastBreakPosition);
  72. } else {
  73. split << process.left(index);
  74. process.remove(0, index);
  75. }
  76. }
  77.  
  78. ++index;
  79. lastBreakPosition = charLength = 0;
  80. }
  81.  
  82. if (!process.isEmpty()) {
  83. split << process;
  84. }
  85.  
  86. return split;
  87. }
To copy to clipboard, switch view to plain text mode 
This is how it's done in the socket class
Qt Code:
  1. void write(const QString &data)
  2. {
  3. kDebug() << socket->write(data.toLatin1());
  4. kDebug() << socket->write("\r\n");
  5. }
  6.  
  7. void
  8. Socket::rfcPrivmsg(const QString &destination, const QString &message)
  9. {
  10. rfcRaw("PRIVMSG " + destination + " :" + codec()->fromUnicode(message));
  11. }
To copy to clipboard, switch view to plain text mode 
Heres how it looks from Konversation
[20:48] <comawhite> ィング株式会社スクウェア・エニックス・ホールディングス 株式会社スクウェア・エニックス・ホールディングス 株式会社スクウェア・エニックス・ホールディングス 株式会社スクウェア・エニックス・ホールディングス 株式会社スクウェア・エニックス・ホールディングス 株式会社スクウェア・エニックス・ホー
[20:48] <comawhite> ールデã⠚£ãƒ³ã‚°ã‚£ ングæ ªå¼ä¼šç¤¾ã†¹ã‚¯ã‚¦ã‚§ Ã£â€šÂ¢Ã¯Â½Â¥Ã£â€šÂ¨Ã£Æ â€¹Ã£Æ’Æ’Ã£â€šÂ¯Ã£â€šÂ ¹Ã£Æ’Â»Ã£Æ’â€ºÃ£Æ’Â¼Ã£Æ ’«ãƒ‡ã‚£ãƒ ã‚°ã‚¹ æ ªå¼ä¼šç¤¾ã†¹ã‚¯ã‚¦ã‚§ Ã£â€šÂ¢Ã¯Â½Â¥Ã£â€šÂ¨Ã£Æ â€¹Ã£Æ’Æ’Ã£â€šÂ¯Ã£â€šÂ ¹Ã£Æ’Â»Ã£Æ’â€ºÃ£Æ’Â¼Ã£Æ ’«ãƒ‡ã‚£ãƒ ã‚°ã‚¹ æ ªå¼ä¼šç¤¾ã†¹ã‚¯ã‚¦ã‚§ Ã£â€šÂ¢Ã¯Â½Â¥Ã£â€šÂ¨Ã£Æ â€¹Ã£Æ’Æ’Ã£â€šÂ¯Ã£â€šÂ ¹Ã£Æ’Â»Ã£Æ’â€ºÃ£Æ’Â¼Ã£Æ ’«ãƒ‡ã‚£ãƒ ã‚°ã‚¹ æ ªå¼ä¼šç¤¾ã†¹ã‚¯ã‚¦ã‚§ Ã£â€šÂ¢Ã¯Â½Â¥Ã£â€šÂ¨Ã£Æ â€¹Ã£Æ’Æ’Ã£â€šÂ¯Ã£â€šÂ ¹Ã£Æ’Â»Ã£Æ’â€ºÃ£Æ’Â¼Ã£Æ ’«ãƒ‡ã‚£ãƒ ã‚°ã‚¹ æ ªå¼ä¼šç¤¾ã†¹ã‚¯ã‚¦ã‚§ Ã£â€šÂ¢Ã¯Â½Â¥Ã£â€šÂ¨Ã£Æ â€¹Ã£Æ’Æ’Ã£â€šÂ¯Ã£â€šÂ ¹Ã£Æ’Â»Ã£Æ’â€ºÃ£Æ’Â¼Ã£Æ ’«ãƒ‡ã‚£ãƒ ã‚°ã‚¹ æ ªå¼ä¼šç¤¾ã†¹ã‚¯ã‚¦ã‚§ Ã£â€šÂ¢Ã¯Â½Â¥Ã£â€šÂ¨Ã£Æ â€¹Ã£Æ’Æ’Ã£â€šÂ¯Ã£â€šÂ ¹Ã£Æ’»ãƒ
[20:48] <comawhite> ルディング株式会社スクウェア・エニックス・ホールディングス 株式会社スクウェア・エニックス・ホールディングス 株式会社スクウェア・エニックス・ホールディングス 株式会社スクウェア・エニックス・ホールディングス 株式会社スクウェア・エニックス・ホールディングス 株式会社スクウェア・エニックス・ホ