Results 1 to 8 of 8

Thread: Converting u_char to QString

  1. #1
    Join Date
    Sep 2006
    Posts
    46
    Thanks
    2
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Converting u_char to QString

    Hello,

    I have searched the forums here and could not find an answer and what I could find on the Internet was run through translators destroying meaning.

    I need to convert a (const u_char *varname) into a QString and then back again. I scoured the Qt4 docs and could not find anything relating to a u_char or conversion method.

    What I could translate from some forums was to use reinterpret_cast<> but could not implement it correctly in this situation.

    Any help would be appreciated.
    Last edited by merlvingian; 28th September 2006 at 02:50. Reason: Fatfingered ** ment *

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Converting u_char to QString

    Qt Code:
    1. QString string = QString((const char*)varname); // directly from the string
    2. QString string = QString::fromLocal8Bit((const char*)varname); // or using local encoding
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Sep 2006
    Posts
    46
    Thanks
    2
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Converting u_char to QString

    I guess I am a newbie Sorry for posting in the wrong section.

    Thank you for the response and will try it out once I get back to a machine where I can access Qt. But from the look of the code should it not throw an error on char to u_char conversion?

    I guess my question then evolves into using depriciated c style type casting in a c++ program. QString seems very flexible does anyone know why such trickery is needed for something as common as a uchar?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Converting u_char to QString

    Quote Originally Posted by merlvingian View Post
    I guess I am a newbie Sorry for posting in the wrong section.
    No problem.

    But from the look of the code should it not throw an error on char to u_char conversion?
    No. u_char is unsigned char, so casting u_char* to const char* is perfectly legitimate.

    I guess my question then evolves into using depriciated c style type casting in a c++ program. QString seems very flexible does anyone know why such trickery is needed for something as common as a uchar?
    Probably the conversion between u_char* and const char* is not handled by your compiler automatically.

    And it's not that common to hold strings in u_char*. char* (or const char*) is the most straightforward container for character strings.

  5. #5
    Join Date
    Sep 2006
    Posts
    46
    Thanks
    2
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Converting u_char to QString

    Thank you again for the reply.

    Couldn't wait so I switched to a machine I could test this out on.

    The problem I see now is as follows:

    The u_char data I am converting to a QString is network data is why I need the u_char 0-225 etc etc. My guess is the char typecast is not going to lose any accuaracy?

    When I access the QString with .toAscii or .toLatin1 I get an output typical of an improperly index array.

    What I am looking to accomplish is taking network data and storing it in an xml file. I must be able to convert it to a QString for storage and *should* be able to read it back as already formatted QString text when needed to display.

    Am I accessing the QString wrong? Is there a conversion I am missing?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Converting u_char to QString

    Is the network data a character string? Or some custom data? Because if the latter, there is no sense using QString for that as QString is suited for characters (i.e. it uses Unicode). If you just want a dynamic array, use QByteArray or QVector.

  7. #7
    Join Date
    Sep 2006
    Posts
    46
    Thanks
    2
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Converting u_char to QString

    Quote Originally Posted by wysota View Post
    Is the network data a character string? Or some custom data? Because if the latter, there is no sense using QString for that as QString is suited for characters (i.e. it uses Unicode). If you just want a dynamic array, use QByteArray or QVector.
    The network data is from LibPcap (winpcap at this point). It appeared that QByteArray was better suited for this situation by I am learning as I go with Qt.

    Just recently finished C++ GUI programming with Qt4, and desgin patterns in Qt4 but I am still very new to this and I appriciate the help you have provided greatly.

  8. #8
    Join Date
    Sep 2006
    Posts
    46
    Thanks
    2
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Converting u_char to QString

    Thank you again Wysota,

    I spent so much time trying to out think the problem in Qt I lost sight of what I was trying to accomplish

    Qt Code:
    1. QString str;
    2.  
    3. for (int i=0;i<lengthofthenetworkdataarray;i++)
    4. str += QString::number(somepacketdata[i],16);
    To copy to clipboard, switch view to plain text mode 

    Did exactly what I needed. Thank you again for taking the time to help a newbie.
    Last edited by merlvingian; 29th September 2006 at 00:12. Reason: variable missmatch, guess this forum does moonlight as an IDE :D

Similar Threads

  1. QString to char* not converting
    By DPinLV in forum Qt Programming
    Replies: 17
    Last Post: 6th August 2006, 12:15
  2. QSqlQueryModel + set Write
    By raphaelf in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2006, 08:55
  3. dialog box
    By Bahar in forum Qt Programming
    Replies: 3
    Last Post: 31st January 2006, 14:52
  4. [SOLVED] Widget plugin ... how to ?
    By yellowmat in forum Newbie
    Replies: 10
    Last Post: 29th January 2006, 20:41
  5. How to create custom slot in Qt Designer 4.1?
    By jamadagni in forum Qt Tools
    Replies: 31
    Last Post: 18th January 2006, 20:46

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.