toBase64 encoding in Qt does not result in a printable QString - c++

I've replicated this in two places in my code, one written by me and the one I'm posting an image of, that was written by someone else. I can't get the base64 to output to qDebug at all. I thought base64 was supposed to be readable. It has a size. But it won't print the entire qDebug line.
Thanks in advance for any help.
Here's the code. I'm on Qt Kit 5.12.1 64 bit mingw in release.
QFile* file = new QFile("C:\\Qr-Pic\\Poll_Directory\\IMG_00000001 - Copy (53).jpg");
file->open(QIODevice::ReadOnly);
QByteArray image = file->readAll();
int originalSize = image.length();
QString encoded = QString(image.toBase64());
int encodedSize = encoded.size();
qDebug() << "encodedSize=" << encodedSize;
qDebug() << "encode=" << encoded;
Output:
encodedSize= 34036

Related

QByteArray conversion from text

I have a textedit where I key in the hex number, and then this text to be converted into QByteArray.
This is my code:
QByteArray parsedValue = QByteArray::fromHex(expectedPacketStr.toUtf8());
qDebug() << parsedValue;
when I set it to 001102,
then console log reports "\x00\x11\x02" which is what I expected.
But if i set it to 001122,
the console logs reports "\x00\x11\"" which is missing the x22 byte.
I really cannot understand what's going on. Anybody have any clue why is this so???
0x22 is the character " in ascii, so it's just qDebug() that is interpreting it, and nothing is missing inside QByteArray.
To convince you, you can always display the array one by one:
for (auto b : parsedValue)
qDebug() << (int)b;

Convert QByteArray from big endian to little endian

I think I’m a kind of at a loss here. I trying such a simple thing, I can’t believe that there is nothing build-in Qt (using Qt 5.6.2). I try to convert the data inside a QByteArray from big endian to little endian. Always starts with the same test QByteArray like this.
QByteArray value;
value.append(0x01);
value.append(0x02);
value.append(0x03);
qDebug() << "Original value is: " << value.toHex(); // “010203” like expected
What I need is the little endian, which means the output should be “030201”. Is there any build in thing so far in the Qt Framework? I don’t find one. What I tried so far
// Try with build in QtEndian stuff
QByteArray test = qToLittleEndian(value);
qDebug() << "Test value is: " << test.toHex(); // 010203
// Try via QDataStream
QByteArray data;
QDataStream out(&data, QIODevice::ReadWrite);
out.setByteOrder(QDataStream::LittleEndian);
out << value;
qDebug() << "Changed value is: " << data.toHex(); // "03000000010203"
Any good idea? Or do I really need to shift hands by hand? Found nothing helpfull on SO or on google or maybe ask the wrong question...
It sounds like you want to reverse the array, rather than manipulate the endianness of any of the multi-byte types inside the array. The standard library has a solution for this:
QByteArray arr;
std::reverse(arr.begin(), arr.end());
// arr is now in reversed order

QT diacritic characters from Base64

I have a problem with getting diacritic characters from strings encoded in Base64 under QT. I'm creating string then I'm encoding it with Base 64 and I'm saving it to file. Next I want to decode characters from opened file. Here is how I do this.
void MainWindow::on_treeWidget_2_doubleClicked(const QModelIndex &index){
encode("([ęśćźół35:11");
decode();
}
void MainWindow::encode(QString input){
QString item_to_change = input.toUtf8().toBase64();
QString filename="output.txt";
QFile file( filename );
if ( file.open(QIODevice::ReadWrite) )
{
QTextStream stream( &file );
stream << encoded;
}
}
void MainWindow::decode(){
QFile input("output.txt");
if (input.open(QIODevice::ReadOnly)) {
data_in = input.readAll();
}
QString strRestored(QByteArray::fromBase64(data_in));
qDebug() << strRestored;
}
Doing this I'm getting only ([esczol35:11 instead ([ęśćźół35:11
Please help me to get all chars as entered at the beginning.
Thanks
I suspect the problem isn't in the Base64 encoding/decoding because that's going to return exactly what you put in every time; that's the point of it. My belief is that your call to your "encode" method is converting the string literal, or that the toUtf8 is converting it incorrectly. Print out each of those steps using qDebug to see where the problem is, and then you'll probably need to look at the QTextCodec class to set up the conversions properly.

Convert short/dos style path name to full path name

The title says it all. I've seen many solutions to execute the oposite operation but not this way.
I'm using Qt to create a temporary file. If I get its name it will be something on the likes of:
QTemporaryFile tempFile;
tempFile.open();
qDebug() << tempFile.fileName();
// C:/Users/USERNA~1/AppData/Local/Temp/qt_temp.Hp4264
I have tried some solutions such as using QFileInfo:
QFileInfo fileInfo(tempFile);
qDebug() << fileInfo.filePath()
And QDir:
QDir dir(tempFile.fileName());
qDebug() << dir.absolutePath();
Without success.
Is there any solution for this using purely Qt? I know I can navigate the directory tree to find the full name but I was trying to avoid this, especially because of the possibility of two folders with the same prefix.
the win32 function GetLongPathName is your answer.
QString shortPath = tempFile.fileName();
int length = GetLongPathNameW(shortPath.utf16(),0,0);
wchar_t* buffer = new wchar_t[length];
length = GetLongPathNameW(shortPath.utf16(), buffer, length);
QString fullpath = QString::fromUtf16(buffer, length);
delete[] buffer;
there is no pure Qt function for this because only windows does this (and Qt is meant to be portable)

Exiv2: How to read photo with UTF8 filepath?

I am using GTKmm and exiv2 to read EXIF metadata form photos. However Exiv2 functions accept only std::string file paths... When I try it on not ASCII filepath it crushes the program.
Is there any way to read that data? It would be great if Exiv2 accepted Glib::ustrings...
I'm interested in solutions for Windows and Linux.
Ok, I have a solution!
You just need to use function Glibmm::locale_from_utf8 to convert UTF8 string to std(ascii) string.
Here is an example:
void get_exif_data(const Glib::ustring &image_src)
{
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(Glib::locale_from_utf8(image_src));
image->readMetadata();
Exiv2::ExifData &exifData = image->exifData();
Exiv2::ExifData::const_iterator it = exifData.begin();
for(it;it!=exifData.end();it++) cout << it->key() + ": " + it->getValue() << endl;
}
If this is in Windows then you can use GetShortPathName.