Results 1 to 10 of 10

Thread: qDebug string display

  1. #1
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default qDebug string display

    Hi,

    I have a function like the following;

    Qt Code:
    1. QString LoggerManager::encryptAes(const char* p_Text, const char* p_key)
    2. {
    3. ...
    4.  
    5. unsigned char* text = new unsigned char((strlen(p_Text) - 1)*sizeof(char));
    6.  
    7. for(int i=0; i < (strlen(p_Text) - 1); i++)
    8. {
    9. text[i] = p_Text[i];
    10. }
    11.  
    12. ...
    13.  
    14. qDebug() << "TextManip 2: " << QString((char*)(text));
    15. }
    To copy to clipboard, switch view to plain text mode 

    p_Text is "ZEROZEROZEROPC010101". But when I want to print it to VS output with qDebug like;

    Qt Code:
    1. qDebug() << "TextManip 2: " << QString((char*)(text));
    To copy to clipboard, switch view to plain text mode 

    the string is printed to output window as;

    "ZEROZEROZEROPC010101?????????"

    Any ideas?

    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qDebug string display

    Did you null-terminate your string?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: qDebug string display

    Try to explicitly add a terminating null character '\0' after the last char of your string in the for loop - i.e p_Text[strlen(p_Text)] = '\0';
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  4. #4
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: qDebug string display

    Hi,

    It's a c-string. So it should have a null character at the end right?

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: qDebug string display

    Should and does may differ - as you are manipulating the string.
    Besides, you are not allocating a C string, you are allocating a char array - its not the same thing!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qDebug string display

    Quote Originally Posted by zgulser View Post
    Hi,

    It's a c-string. So it should have a null character at the end right?
    But You don't copy null character. Look at the condition of the loop execution.

  7. #7
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: qDebug string display

    Yeap... That's why I don't understand why ???????? is appended to the end. The null character should be put at the end of c-strings.

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: qDebug string display

    The null character should be put at the end of c-strings.
    But you are not allocating a C string!
    And we don't know how p_Text is allocated or initialized!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. The following user says thank you to high_flyer for this useful post:

    zgulser (2nd July 2012)

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qDebug string display

    Quote Originally Posted by zgulser View Post
    Yeap... That's why I don't understand why ???????? is appended to the end.
    There is no such thing as a "C string". You have an array of characters that contain textual data. All functions that work on such data expect them to be null terminated and nobody is going to magically discover that if you copy 10 bytes somewhere, you actually want to null-terminate them.

    The null character should be put at the end of c-strings.
    That's true. And since you didn't put that null character there, you should now correct your error.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. The following user says thank you to wysota for this useful post:

    zgulser (2nd July 2012)

  12. #10
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: qDebug string display

    Ok..

    I corrected by putting '\0' at the end.

    However, if I put this,

    openssl's

    Qt Code:
    1. AES_encrypt((unsigned char*)text, (unsigned char*)enc_out, &enc_key);
    To copy to clipboard, switch view to plain text mode 

    doesn't work. I mean enc_out returns "". But if I remove '\0' from text, enc_out returns as expected but with ???????? at the end.


    Added after 9 minutes:


    Ok,

    Adding '\0' at the end of enc_out solved the problem.

    Thanks for your help.
    Last edited by zgulser; 2nd July 2012 at 13:59.

Similar Threads

  1. Replies: 3
    Last Post: 10th November 2010, 13:27
  2. Replies: 0
    Last Post: 14th April 2010, 12:21
  3. display time as as string ?
    By Petr_Kropotkin in forum Newbie
    Replies: 26
    Last Post: 29th January 2010, 15:46
  4. How to display individual character of a string?
    By cooper in forum Qt Programming
    Replies: 2
    Last Post: 15th July 2009, 05:19
  5. qDebug style string insertion
    By rbp in forum Qt Programming
    Replies: 3
    Last Post: 17th March 2009, 03:24

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.