Decode Shift-JIS and display to user interface using QT - qt

I have a code to decode SHIFT-JIS using qt to user interface, here is my code:
QTextCodec *codec1 = QTextCodec::codecForName("Shift-JIS");
QByteArray encoded = "81ff";
QString current = codec->toUnicode(QByteArray::fromHex(encoded));
ui->hientai->setText(current);
And the result of my code is wrong. So please tell me what is the problem?
This is the output to UI

Related

Generate QByteArray format with Golang

I have an application on QT which reads from a file with text in a QByteArray format (the information belongs to a certificate stored in a file). Looks like this:
certificate="#ByteArray(0\x82\tF\x2\x1\x3\x30\x82\t\x10\x6\t*\x86H\x86\xf7\r\x1\a\x1\xa0\x82\t\x1\x4\x82\b\xfd\x30\x82\b\xf9\x30\x82\x3\xad\x6\t*\x86H\x86\xf7\r\x1\a\x1\xa0\x82\x3\x9e\x4\x82\x3\x9a\x30\x82\x3\x96\x30\x82\x3\x92\x6\v*\x86H\x86\xf7\r\x1\f\n\x1\x3\xa0\x82\x3+0\x82\x3'\x6\n*\x86H\x86\xf7\r\x1\t\x16\x1\xa0\x82\x3\x17\x4\x82\x3\x13\x30\x82\x3\xf\x30\x82\x1\xf7\xa0\x3\x2\x1\x2\x2\x1\x1\x30\r\x6\t*\x86H\x86\xf7\r\x1\x1\x5\x5\0\x30\x16\x31\x14\x30\x12\x6\x3U\x4\x3\f\vMumble User0\x1e\x17\r200130204841Z\x17\r400125204841Z0\x16\x31\x14\x30\x12\x6\x3U\x4\x3\f\vMumble User0\x82\x1\"0\r\x6\t*\x86H\x86\xf7\r\x1\x1\x1\x5\0\x3\x82\x1\xf\0\x30\x82\x1\n\x2\x82\x1\x1\0\xc6\x1d\x66\x8f\xc2\x31-N\xa1\xa0,\xf4\xb9\xc6\x80\x30)\xba\xf6\x17\x37\xec\x82\x1a\xf2s\xfc\xbc\x86\x93\x97\x83G\xccU(\vnr\t\xa8\x61\x12\b\x8f\xd6\x95T?\xc9\xe8\xe3v\x80%\xe3}\xe6,\x90\a\xe2\x18\xe0V\x80k\xee\x88&H <2G\x81\xec\xd6\xf1\xe9\xd0\xcat\x13\x9c?\x81\xcf\xaf\x14J\xc1X\xa5k\x9f\xf6P\xd9y\b\xa2\xe7\xaf\xa0l\xa0\xb4\xc7g\xc3*\xdd\x10\x16\xde\xe\xc3
Now I need generate the same format (QByteArray) using a GO application, but didn't find how generate this format.
Anyone can point me to some example or documentation on how create the same format of a QByteArray using another language (go)?
Thanks in advance.
QByteArray is just an array of bytes, in go that would just be []byte

QUrl and encoding

I'm having a problem on mac with Qt 4.8.x
I have a mailto QURL which has a body component that's encoded. Simplifying it:
mailto:?subject=Hello&body=Hello%2C%0A%0AYou%20have%20been%20invited
My problem is that the encoding still shows on the mail body when my default app launches (tried multiple e-mail clients). This works well for Windows, but fails on Mac.
Is there a way for me to have the encoding turn into the encoded characters into readable text in the e-mail client body?
Also use fromPercentEncoding to decode Qurl format in to clear string.
QUrl is buggy. There is no way around this bug but to go directly to Cocoa.
void MyMacLaunchURLClass::LaunchURL(char * url)
{
NSString *urlStr = [NSString stringWithCString:url encoding:NSASCIIStringEncoding];
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:urlStr]];
}
That resolved the bug.

Converting UTF-16 QByteArray to QString

I have a QByteArray which contains bytes in UTF-16 format.
A Java program sends data to a QT program via socket using
//dos is DataOutPutStream
dos.writeChars("hello world");
On the receiver side in QT program I read the data from socket into QByteArray and I want to convert it to a QString. inspecting the data variable of QByteArray it has 0h0e0l0l0o0 0w0o0r0l0d
When I try to make a QString out of it like this
QString str(byteArray)
The resulting string is empty perhaps because it encounters a 0 byte at the start and ofcouse because the documentation of the constructor I am using says that it internally uses fromAscii and what I am passing is not ascii.
I guess i have to somehow use QString::fromUTF-16 but that requires a ushort* and I have a QbyteArray.
Please advise what is the best way to do it.
Thanks,
Get a pointer to the QByteArray.data() and cast it to ushort*
This would work, assuming your utf-16 data is of the same endianness or has the BOM (Byte Order Mark):
QByteArray utf16 = ....;
auto str = QString::fromUtf16(
reinterpret_cast<const ushort*>(utf16.constData()));

Displaying UTF-8 characters in a PlainTextEdit

I'm trying to display Chinese characters encoded in UTF-8 in a PlainTextEdit control, but it doesn't render them properly.
My data comes from a database and I know that the string I get in Qt is correct (the bytes are the same as in the database). Once I have the Chinese character in a QString, I tried various things to display it but always results in either question marks or random ASCII characters:
QString chineseChar = query.value(fieldNo).toString(); // get the character
ui->plainTextEdit->appendPlainText(chineseChar); // doesn't work
ui->plainTextEdit->appendPlainText(chineseChar.toUtf8()); // doesn't work
ui->plainTextEdit->appendPlainText(QString::fromUtf8(chineseChar.toAscii()); // doesn't work
Any suggestion on how to handle that?
"My data comes from a database and I know that the string I get in Qt is correct (the bytes are the same as in the database)."
How did you check that? Try with chineseChar.toUtf8().toHex().
Once your string data is in a QString, all UI elements accepting a QString will handle it correctly. Usually the error happens when converting from plain text data(const char*/QByteArray) to the QString.
The conversions here:
ui->plainTextEdit->appendPlainText(chineseChar.toUtf8()); // doesn't work
ui->plainTextEdit->appendPlainText(QString::fromUtf8(chineseChar.toAscii()); // doesn't work
convert the unicode string to a bytearray, and then implicitely back to a QString, as those methods expect a QString.
I suggest you define QT_NO_CAST_FROM_ASCII and QT_NO_CAST_TO_ASCII to avoid any unwanted QByteArray<->QString conversions.
If the string is wrong, the error usually happened before, when converting from QByteArray/const char* to QString, i.e. in query.value(fieldNo).toString(). Try with:
QString chineseChar = QString::fromUtf8( query.value(fieldNo).toByteArray() );
If that doesn't help, the problem is somewhere in QtSQL assuming the wrong encoding for the data it receives from the database.

Error-invalid length for a base-64 char array

I have silverlight app that post some data to another web application ,the data to post is converted to base 64 using code
byte[] byteArray = Encoding.UTF8.GetBytes(sDataToPost);
sDataToPost = Convert.ToBase64String(byteArray);
Another webapplication
get the data using the code
strText = System.Text.Encoding.ASCII.GetString(System.Convert.FromBase64String(postedData));
But it gives the exception invalid length for a base-64 char array
Thanks in Advance
DNM
Depending on how you post the data its quite possible that the Base64 string is being munged a little more. For example URL encoders may do odd things with the + and = symbols in the Base64 string.

Resources