Results 1 to 8 of 8

Thread: convert a string to hex

  1. #1
    Join Date
    Feb 2009
    Posts
    16
    Thanks
    2
    Platforms
    Unix/X11

    Default convert a string to hex

    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_string2))
    {
    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??

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: convert a string to hex

    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"
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Feb 2009
    Posts
    16
    Thanks
    2
    Platforms
    Unix/X11

    Unhappy Re: convert a string to hex

    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

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: convert a string to hex

    what is type of compressed_string2?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Feb 2009
    Posts
    16
    Thanks
    2
    Platforms
    Unix/X11

    Default Re: convert a string to hex

    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.

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: convert a string to hex

    what is content of this string: numbers or characters or numbers and characters?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: convert a string to hex

    try this
    Qt Code:
    1. QString string = (compressed_string2.c_str());
    2. QString res = string.toAscii().toHex();
    3. qDebug() << res;
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. The following user says thank you to spirit for this useful post:

    priceey (23rd February 2009)

  9. #8
    Join Date
    Feb 2009
    Posts
    16
    Thanks
    2
    Platforms
    Unix/X11

    Default Re: convert a string to hex

    thanks!

    thats rock and roll lovely

Similar Threads

  1. How to convert from QString to string ?
    By probine in forum Newbie
    Replies: 2
    Last Post: 1st December 2010, 01:50
  2. convert string to byte values
    By steg90 in forum Qt Programming
    Replies: 1
    Last Post: 22nd November 2007, 14:06
  3. Reading from sockets in a multithreaded program
    By KoosKoets in forum Qt Programming
    Replies: 9
    Last Post: 4th April 2007, 20:43
  4. saving a c string of variable length in a shared memory?
    By nass in forum General Programming
    Replies: 4
    Last Post: 3rd January 2007, 14:40
  5. Convert from QByteArray to String ?
    By probine in forum Newbie
    Replies: 3
    Last Post: 25th March 2006, 15:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.