PDA

View Full Version : Ascii control characters



priceey
23rd February 2009, 15:44
Hello again

I am writing an application that is converting some code from char to ascii to hex as so..



QString string = (compressed_string2.c_str());
QString res = string.toAscii().toHex();
qDebug() << res;


Now my problem is that sometimes I get NULL characters in the code, and therefore I do not parse the rest of the text. for example, my output should be



9c00f57b74


instead I get



9c


I am pretty bad with QT. From my investigation so far, the best I can deduce is to use codecForCStrings but for the life of me I can't work it out.

Anyone have any ideas how I can convert the ascii control characters?

thanks

Priceey

aamer4yu
23rd February 2009, 16:29
What if you try
QString res = string.toUtf8().toHex();

Does it help ?

priceey
23rd February 2009, 16:34
Thanks for the quick reply, but no that doesn't work.

it alters the output to something totally different to what I am expecting

:(

aamer4yu
23rd February 2009, 16:43
What format is your input ?
and also what data type is compressed_string2 ? c_str() doesnt seem to be Qt function

priceey
23rd February 2009, 16:53
My input is in a standard string format:



std::string compressed_string2;


c_str converts a regular string to a QString.

^NyAw^
23rd February 2009, 17:05
Hi,

Hi,

QString::QString ( const QByteArray (file:///C:/Qt/4.3.0/doc/html/qbytearray.html) & ba )

Constructs a string initialized with the byte array ba. The given byte array is converted to Unicode using fromAscii (file:///C:/Qt/4.3.0/doc/html/qstring.html#fromAscii)(). Stops copying at the first 0 character, otherwise copies the entire byte array.


So


QString res = string.toAscii().toHex();
stops at first '0' character

priceey
23rd February 2009, 17:23
thanks for the input.

That makes sense.

but if that is the case, then if I add

#define QT_NO_CAST_FROM_ASCII

should add the complete line to the string?

is that true?

it gives the same result if I try it.

^NyAw^
23rd February 2009, 17:53
Hi,

I think that you have to add "48" to each char to obtain the desired character. Take a look at ASCII table where you can see that character "0" is 48 decimal. So this is basically a LUT.
Try it and tell if it helps you.

priceey
24th February 2009, 11:57
thanks for all of your help

here is how I fixed it:



QString compressedString1 = QString::fromStdString(compressed_string1);
QString compressedString1Q = compressedString1.toAscii().toHex();


this seemed to fix the issue.