Results 1 to 3 of 3

Thread: Problem with type casting?

  1. #1
    Join Date
    Aug 2009
    Posts
    3
    Thanks
    2
    Platforms
    MacOS X

    Question Problem with type casting?

    Hi

    I am new in QT.
    I am trying to convert text string to hexadecimal.

    Qt Code:
    1. void string2hex::browse()
    2. {
    3. QString str="abcd";
    4. QString res;
    5. res = String2Hex(str);
    6. QMessageBox::information(this,tr("Output is"),res);
    7. }
    8.  
    9. QString string2hex:: Byte2Hex(unsigned char c)
    10. {
    11. //code which convert bytes into hex
    12. }
    13. QString string2hex:: String2Hex(QString S)
    14. {
    15. unsigned int StrLen = S.length();
    16. QString Result;
    17.  
    18. for (unsigned int i = 0; i < StrLen; i++)
    19. Result += Byte2Hex((unsigned char)S[i]);
    20. return Result;
    21. }
    To copy to clipboard, switch view to plain text mode 
    I gives the compilation error
    string2hex.cpp: In member function ‘QString string2hex::String2Hex(QString)’:
    string2hex.cpp:53: error: invalid cast from type ‘QCharRef’ to type ‘unsigned char’
    So can anyone tell me how to do this type casting?

    thanks in advance.
    Last edited by wysota; 25th August 2009 at 09:01. Reason: Changed [quote] to [code]

  2. #2
    Join Date
    May 2006
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with type casting?

    Have a look at the [] operator for a QString in the Qt documentation. It returns a const QChar instead of an unsigned char.

    Try:

    Qt Code:
    1. S[i].toAscii()
    To copy to clipboard, switch view to plain text mode 

    The toAscii() method will return a char which you need.

  3. #3
    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: Problem with type casting?

    How about using QByteArray ? It already has QByteArray::toHex.
    Also conversion between QString and QByteArray is a step away.

Similar Threads

  1. Template classes and abstraction of type
    By Raistlin in forum General Programming
    Replies: 15
    Last Post: 1st April 2008, 11:18
  2. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 11:35
  3. problem with forward declaration
    By MarkoSan in forum General Programming
    Replies: 14
    Last Post: 6th January 2008, 22:45
  4. quint16 compiling problem
    By MarkoSan in forum Qt Programming
    Replies: 5
    Last Post: 30th November 2007, 19:08
  5. Replies: 16
    Last Post: 7th March 2006, 16:57

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.