Image loading error from base64 string Qt - c++

string text = "Some base64 string";
QByteArray bytes = QByteArray::fromBase64(text.c_str());
cover.loadFromData(bytes);
This is my code. It's work fine, when base64 string starts with "iV". But in another case (for example when it's start with "/9j/") cover returns NULL. What's the problem?

Related

Convert QByteArray to QString

I want to encrypt the data of a database and to do this, I used AES_128 in this link for encryption.
The result of encryption is a QByteArray and the QByteArray is saved on the text file in the correct shape and I could decode it correctly, but and I need to convert it to the QString and reverse to QByteArray to store and read it on the Sqlite DB. I tried some options like
QByteArray encodedText; QString DataAsString = QString(encodedText);
and
string DataAsString1 = encodedText.toStdString();
and
QString DataAsString = QTextCodec::codecForName("UTF-8") >toUnicode(encodedText);
and other solutions like this link, but the outputs of these options aren't in the correct way. Because after casting, I couldn't convert the encoded text to decoded correctly.
This is the input string of encoded text:
"\x14r\xF7""6#\xFE\xDB\xF0""D\x1B\xB5\x10\xEDx\xE1""F"
and these are the outputs for the different options:
\024r�6#���D\033�\020�x�F
and
\024r�6#���D\033�\020�x�F
Does anybody suggestion about the right conversion?
try to use this:
QString QString::fromUtf8(const QByteArray &str)

string in webclient class c# changed to uncorrect format

I call my service in wcf as you can see :
ClientRequest.Headers["Content-type"] = "application/json";
string result = ClientRequest.DownloadString(ServiceHostName + "/NajaService.svc/GetCarData/" + plaque);
var javascriptserializer = new JavaScriptSerializer();
return javascriptserializer.Deserialize<NajaResult>(result);
But the returned data is like this :
{"CarColor":"آبي سير","CarModel":"1383","CarTip":"ال ايکس","CarType":"سواري","Chassis":"83844131","Family":"####","FuelType":1,"MotorNum":"12483068683","Name":"####","NationalCode":"0000000000","Plaque":"11-426د61","PlaqueCoded":110561426,"PlaqueType":"","SystemType":"سمند","VinNo":"IRFC831V3GJ844131"}
I converted it to UTF8 byte and again convert it to utf8 string but not solved.
The encoded data is in Persian language .
I traced the request in fiddler and i found that the data is come with the correct format as you can see ,But in my code is changed
The WebRequest contains the Encoding property you can set up before downloading the service reply. Details are here: https://msdn.microsoft.com/en-us/library/system.net.webclient.encoding(v=vs.110).aspx

Azure: decoding a string in base64 containing a picture

I am developing a web-app that has to take a picture from a JSON produced by an Azure experiment.
I have the following function.
public static Image Base64ToImage(string base64String)
{
try
{
byte[] imageBytes = System.Text.Encoding.UTF8.GetBytes(base64String);//Convert.FromBase64String(base64String);
MemoryStream ms1 = new MemoryStream(imageBytes,0,imageBytes.Length);
ms1.Write(imageBytes, 0, imageBytes.Length);
Image NewImage = Image.FromStream(ms1, true);
NewImage.Save(#"C:\TESTOK.jpg");
return NewImage;
}
catch (Exception ex)
{
string appo = ex.Message.ToString() + ex.InnerException.ToString();
return null;
}
}
It receives the string produced by the Azure webservice (a codified image). When I try to create the image through the function Image.FromStream(ms1, true); it produces an exception returning the error:
"Parameter is not valid".
The input string is the following.
iVBORw0KGgoAAAANSUhEUgAAAeAAAAHgCAMAAABKCk6nAAAADFBMVEX9/v0AAAAAzQD////A95lXAAAABHRSTlP/////bShGeQAAC+xJREFUeJztnYl22yoUAIP1///cprYQm3bZRtOZ856NEFyWsZZgNfkZBM3Ptzsg70XBcBQMR8FwFAxHwXAUDEfBcLoRHEKoUq/NNBFe5Lv2NLOrSNLYTMnNfSgHmI1ktanD9CQ45Im4I7odpmkJ467dzewpUkpolNwjOBtgyIOvNHWYGwmOW0N1lF/ck6RPi43t6kQtuNHG9eO6qeAjB+/2nmxt7ArBQ34yIguOJ+JJ6Sv1PHdl0xILvXYXExhiyHhkpBPZKhIza8FD3D9UJbM+jKVamooBVvcWRyduBYDgcW9+bZtSIc9bLpJfVUOerutkfWjdOs0OsBD8tovOLQQPqZup/Di5w/SaprLMmSpzRaZeJUdmfCm7Vre+X/Aw87k4C0DwMDT0lDHKalWR53spOGY2T9d1H5LQKwOsBE9NXUpPglO5JwQX59p45syqDVXt1im6aCtuVqfoRuhYsj3AluBy4wr+E8HZgTh3kGcRW20NSaRrBbdtX8FdBRe2qinOM1+bobJQ1tsmuOzarOCFAdYjma15iq4ED3/HP2RK5wS3bo7ikThUu0onWfBWg0PSyp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BcQbHGRxncJzBcQbHGRxncJzBcQbHG
RxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRx3L2yELnclpkkAAAAASUVORK5CYII=
I have tried to decode the image from command-line:
$ base64 -d string > stringDecoded
base64: invalid input
$ base64 -d -i string > stringDecoded
$
where the parameter -i stands for --ignore-garbage: *when decoding, ignore non-alphabet characters. So, this is the issue to solve.
For Windows you can do the same thing using decb64:
> dec64 stringa.txt
So it seems clear that the problem is that my string contains non-alphanumeric characters. How can I modify my code to solve this issue?
I have understood why no one gave me an answer: there was no answer.
Some days ago the AzureML developers modified the structure of the JSON containing the image. Now it appears like this:
It could be interesting to know why it didn't work with the previous version. Nevertheless, the problem is solved!

Detect text file encoding

In my program I load plain text files supplied by the user:
QFile file(fileName);
file.open(QIODevice::ReadOnly);
QTextStream stream(&file);
const QString &text = stream.readAll();
This works fine when the files are UTF-8 encoded, but some users try to import Windows-1252 encoded files, and if they have words with special characters (for example "è" in "boutonnière"), those will show incorrectly.
Is there a way to detect the encoding, or at least distinguish between UTF-8 (possibly without BOM), and Windows-1252, without asking the user to tell me the encoding?
Turns out that auto-detecting the encoding is impossible for the general case.
However, there is a workaround to at least fall back to the system locale if the text is not valid UTF-8/UTF-16/UTF-32 text. It uses QTextCodec::codecForUtfText(), which tries to decode a byte array using UTF-8, UTF-16 and UTF-32, and returns the supplied default codec if it fails.
Code to do it:
QTextCodec *codec = QTextCodec::codecForUtfText(byteArray, QTextCodec::codecForName("System"));
const QString &text = codec->toUnicode(byteArray);
Update
The above code will not detect UTF-8 without BOM, however, as codecForUtfText() relies on the BOM markers. To detect UTF-8 without BOM, see https://stackoverflow.com/a/18228382/492336.
This trick works for me, at least so far. This method does not require BOM to work:
QTextCodec::ConverterState state;
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
const QByteArray data(readSource());
const QString text = codec->toUnicode(data.constData(), data.size(), &state);
if (state.invalidChars > 0)
{
// Not a UTF-8 text - using system default locale
QTextCodec * codec = QTextCodec::codecForLocale();
if (!codec)
return;
ui->textBrowser->setPlainText(codec->toUnicode(readSource()));
}
else
{
ui->textBrowser->setPlainText(text);
}

How do I read binary C++ protobuf data using Python protobuf?

The Python version of Google protobuf gives us only:
SerializeAsString()
Where as the C++ version gives us both:
SerializeToArray(...)
SerializeAsString()
We're writing to our C++ file in binary format, and we'd like to keep it this way. That said, is there a way of reading the binary data into Python and parsing it as if it were a string?
Is this the correct way of doing it?
binary = get_binary_data()
binary_size = get_binary_size()
string = None
for i in range(len(binary_size)):
string += i
message = new MyMessage()
message.ParseFromString(string)
Update:
Here's a new example, and a problem:
message_length = 512
file = open('foobars.bin', 'rb')
eof = False
while not eof:
data = file.read(message_length)
eof = not data
if not eof:
foo_bar = FooBar()
foo_bar.ParseFromString(data)
When we get to the foo_bar.ParseFromString(data) line, I get this error:
Exception Type: DecodeError
Exception Value: Too many bytes when decoding varint.
Update 2:
It turns out, that the padding on the binary data was throwing protobuf off; too many bytes were being sent in, as the message suggests (in this case it was referring to the padding).
This padding comes from using the C++ protobuf function, SerializeToArray on a fixed-length buffer. To eliminate this, I have used this temproary code:
message_length = 512
file = open('foobars.bin', 'rb')
eof = False
while not eof:
data = file.read(message_length)
eof = not data
string = ''
for i in range(0, len(data)):
byte = data[i]
if byte != '\xcc': # yuck!
string += data[i]
if not eof:
foo_bar = FooBar()
foo_bar.ParseFromString(string)
There is a design flaw here I think. I will re-implement my C++ code so that it writes variable length arrays to the binary file. As advised by the protobuf documentation, I will prefix each message with it's binary size so that I know how much to read when I'm opening the file with Python.
I'm not an expert with Python, but you can pass the result of a file.read() operation into message.ParseFromString(...) without having to build a new string type or anything.
Python strings can contain any character, i.e. they are capable of holding "binary" data directly. There should be no need to convert from string to "binary".