Results 1 to 9 of 9

Thread: Ascii control characters

Hybrid View

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

    Default Ascii control characters

    Hello again

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

    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 

    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

    Qt Code:
    1. 9c00f57b74
    To copy to clipboard, switch view to plain text mode 

    instead I get

    Qt Code:
    1. 9c
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Ascii control characters

    What if you try
    QString res = string.toUtf8().toHex();

    Does it help ?

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

    Default Re: Ascii control characters

    Thanks for the quick reply, but no that doesn't work.

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


  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Ascii control characters

    What format is your input ?
    and also what data type is compressed_string2 ? c_str() doesnt seem to be Qt function

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

    Default Re: Ascii control characters

    My input is in a standard string format:

    Qt Code:
    1. std::string compressed_string2;
    To copy to clipboard, switch view to plain text mode 

    c_str converts a regular string to a QString.
    Last edited by priceey; 23rd February 2009 at 15:59.

  6. #6
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Ascii control characters

    Hi,

    Hi,

    QString::QString ( const QByteArray & ba )

    Constructs a string initialized with the byte array ba. The given byte array is converted to Unicode using fromAscii(). Stops copying at the first 0 character, otherwise copies the entire byte array.


    So
    Qt Code:
    1. QString res = string.toAscii().toHex();
    To copy to clipboard, switch view to plain text mode 
    stops at first '0' character
    Òscar Llarch i Galán

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

    Default Re: Ascii control characters

    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Ascii control characters

    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.
    Òscar Llarch i Galán

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

    Default Re: Ascii control characters

    thanks for all of your help

    here is how I fixed it:

    Qt Code:
    1. QString compressedString1 = QString::fromStdString(compressed_string1);
    2. QString compressedString1Q = compressedString1.toAscii().toHex();
    To copy to clipboard, switch view to plain text mode 

    this seemed to fix the issue.

Similar Threads

  1. How to Compare Characters ASCII value?
    By merry in forum Qt Programming
    Replies: 5
    Last Post: 14th July 2008, 12:05
  2. Replies: 4
    Last Post: 10th July 2007, 20:11
  3. Problem at time compilation in traslation of language
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 22nd May 2007, 14:18
  4. Replies: 6
    Last Post: 3rd February 2006, 09:48

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
  •  
Qt is a trademark of The Qt Company.