PDA

View Full Version : convert a string to hex



priceey
12th February 2009, 09:38
Hello peeps

can someone help me please?

I have tried 5000 ways, but at no point can I do this.

I have a string, which has been encoded.

Basically I want to convert the string to a hex value to confirm it has worked (i am playing around with some encoding)

here is my code:


std::string compressed_string2;
if (false ==
encoder_table2.Encode(test_string2,compressed_stri ng2))
{
std::cerr << "Error compressing string 2" << std::endl;
exit(1);
}


what i need to do is to print the result of that code to a hex value.

Can someone please point me in the right direction?

I have tried to put the string into a QByteArray, then converting that into hex, but I keep getting some error regarding the char is no good.

Can someone please help me??

spirit
12th February 2009, 09:56
did you see these methods?


QByteArray QByteArray::toHex () const
QString QString::number ( long n, int base = 10 )

there is an example in Qt Assistant


long a = 63;
QString s = QString::number(a, 16); // s == "3f"
QString t = QString::number(a, 16).toUpper(); // t == "3F"

priceey
12th February 2009, 10:26
I am pretty new to QT, so please forgive my poor post

Here is what I tried:


QString string = (compressed_string2.c_str());
long string2 = string.toLong();
QString s = QString::number(string,16);


when i try this, string1 is zero. so i am unsure how i can use the code, because the input needs to be long for the QString::number to work.

And as for the QByteArray, no matter how I try it, I can't get it to convert the string

if i am honest with you i dont really have a clue how to get the QByteArray to work

spirit
12th February 2009, 10:36
what is type of compressed_string2?

priceey
12th February 2009, 10:45
compressed_string2 is a plain text string that has been compressed using huffman encoding.

basically this should be a hex string (i think)

at the moment all i want to do is print this string out as hex, so i can confirm that the string has been compressed correctly.

spirit
12th February 2009, 11:02
what is content of this string: numbers or characters or numbers and characters?

spirit
12th February 2009, 11:11
try this


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

priceey
12th February 2009, 11:38
thanks!

thats rock and roll lovely