Results 1 to 5 of 5

Thread: convert char to hex, please help.. [Solved]

  1. #1
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question convert char to hex, please help.. [Solved]

    hi everyone,

    i got a string from anther computer through serial port. i need to decode the character into hex value.

    for example:

    i get string "ABC", i need to calculate 0x41 + 0x42 + 0x43

    how can i get the value of "A" in hex or in dec?

    thanks in advance
    Last edited by cooper; 16th July 2009 at 02:44.

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: convert char to hex, please help..

    Qt Code:
    1. QString s="ABC";
    2. int len=s.length();
    3. int sum=0;
    4. QChar ch;
    5. for(int i=0;i<len;i++)
    6. {
    7. ch= s.at(i);
    8. sum+=ch.unicode();
    9. }
    To copy to clipboard, switch view to plain text mode 

  3. The following 2 users say thank you to nish for this useful post:

    cooper (16th July 2009), zeFree (30th April 2013)

  4. #3
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: convert char to hex, please help..

    Quote Originally Posted by MrDeath View Post
    Qt Code:
    1. QString s="ABC";
    2. int len=s.length();
    3. int sum=0;
    4. QChar ch;
    5. for(int i=0;i<len;i++)
    6. {
    7. ch= s.at(i);
    8. sum+=ch.unicode();
    9. }
    To copy to clipboard, switch view to plain text mode 
    thanks Mr Death, it works.
    i did not realize that the unicode is a number in decimal format, i need to go back to school

  5. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: convert char to hex, please help..

    even i need to go back to school

  6. #5
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    23
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Lightbulb Re: convert char to hex, please help..

    A better (or should I say a perfect) example is available at Nokia Developer Community Wiki.
    It goes like this:
    Qt Code:
    1. /* hexadecimal 0xED8788DC is equivavelent to decimal 3985082588 */
    2. QString str = "0xED8788DC";
    3. bool ok;
    4. uint appId = str.toUInt(&ok,16); //appId contains 3985082588
    5.  
    6. /* decimal 3985082588 is equivalent to hex ED8788DC */
    7. uint decimal = 3985082588;
    8. QString hexadecimal;
    9. haxadecimal.setNum(decimal,16); //now hexadecimal contains ED8788DC
    To copy to clipboard, switch view to plain text mode 
    I hope this will help.

Similar Threads

  1. Char array[6] to QString and display it
    By arunvv in forum Newbie
    Replies: 11
    Last Post: 12th March 2014, 20:48
  2. convert unsigned char * to QString
    By sepehr in forum Qt Programming
    Replies: 4
    Last Post: 9th December 2008, 20:31
  3. convert from qstring to const char *
    By deepa.selvaraj in forum Qt Programming
    Replies: 3
    Last Post: 28th November 2007, 12:33
  4. How to convert QString to char *
    By rajeshs in forum Qt Programming
    Replies: 7
    Last Post: 27th September 2007, 10:32
  5. unable to save QCStrings properly in a buffer
    By nass in forum Qt Programming
    Replies: 13
    Last Post: 15th November 2006, 20: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.