Results 1 to 8 of 8

Thread: How to access QString characters values

  1. #1
    Join Date
    Nov 2009
    Posts
    29
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to access QString characters values

    Hello,

    I am working in a program and I need to convert a integer to a binary number, and then do different stuff depending on the binary values of that number. My question is, how can I access to the individual characters of a QString binary representation of a number?

    My code goes something like this:

    Having:

    Qt Code:
    1. int mynumber;
    To copy to clipboard, switch view to plain text mode 

    I convert that int to binary using:

    Qt Code:
    1. QString mybinary(QString::number(mynumber, 2));
    To copy to clipboard, switch view to plain text mode 

    Now, how to know the value of each QString character? to do something like this:

    Qt Code:
    1. if(mybinary[1]==0) other_variable=x;
    To copy to clipboard, switch view to plain text mode 

    Can I do even mathematics operations such as?:

    Qt Code:
    1. mybinary[0]*other_variable/Pi;
    To copy to clipboard, switch view to plain text mode 



    I hope you can understand what I mean.

    Thank you very much!

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to access QString characters values

    QString has operator[] so the call myQString[0] returns the first QChar from QString, for the calculation part you need to convert the QChar to int.

    And i think you should tell more about what are you trying to achieve, because this conversions from int to QString, back to int doesn't sound like a good design, maybe you should find another container for your bits, like for example the std::bitset, or i think Qt got to have something similar.

  3. #3
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to access QString characters values

    It looks like what you are needing is bit-mask operations (what Zlatomir was getting at in suggesting std::bitset I think).

  4. #4
    Join Date
    Nov 2009
    Posts
    29
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to access QString characters values

    Hi,

    I'm not sure.

    In my program, I have a Widget with a spinbox on it, that goes from 0 to 255. In that spinbox I input any number. Then, internally, I need to know the binary representation of that number to construct something like a table. Lets say my number is 25, then it is 11001 in binary. Using that binary I have to do something like:

    if lsb is 1, then do A
    else if lsb is 0, then do B.

    if 2nd lsb is 1, then do C.
    else if 2nd lsb is 0, then do D.

    And so it goes.

    Any hints?

    Thank you!

  5. #5
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to access QString characters values


  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to access QString characters values

    Just a note that you can't just convert chars and ints.
    QString::operator[] might not do what you think it does! Remember that QString holds a unicode string. the [] operator will not return a (correct) number to use in mathematical operations.

    So doing:
    Qt Code:
    1. if(mybinary[1]==0)
    To copy to clipboard, switch view to plain text mode 
    where mybinary is a string, is not correct.
    First convert the char pointed at by mybinary[1] to a correct integer representation that you can use.

  7. The following user says thank you to tbscope for this useful post:

    rdelgado (15th November 2010)

  8. #7
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to access QString characters values

    For efficiency you can use some bit operators to extract bits from int, but since you have/need just one string and you don't need that many conversions, basically you can do this with just one conversion from that int to a QString, i think it is not really necessary to "play" with bits //unless you are programming for some embedded system with small amount of memory

    The code in tbscope post doesn't work, but you don't really need to convert the QChar into int again, you can compare what operator[] returns with the character '0' or '1', something like:
    Qt Code:
    1. QString str("010100"); //you have the string with 1 and 0
    2. if(str[0] == '0') // you test the first character if it is character '0'
    3. qDebug() << "first character in the string is zero"; //call the function you need
    To copy to clipboard, switch view to plain text mode 
    you can do it in a loop, or whatever fit your needs

    LE: if you really need the integer back see the link in my first post, also if you don't like the QString approach you can search the Qt documentation for some container for bits (something more or less equivalent for std::bitset) it might be easier to use than bit operators.
    Last edited by Zlatomir; 13th November 2010 at 09:30.

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

    rdelgado (15th November 2010)

  10. #8
    Join Date
    Nov 2009
    Posts
    29
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to access QString characters values

    Hello,

    I guess bitwise operations in way more elegant and efficient, but considering what I want, I think that comparing the QString with '0' and '1' characters will do the trick.
    And I've just tested it and it worked the way I wanted.

    Thank you very much!

Similar Threads

  1. QString with escape characters
    By Jeffb in forum Newbie
    Replies: 1
    Last Post: 26th April 2010, 14:06
  2. Replies: 4
    Last Post: 1st February 2010, 14:21
  3. How to access the lineEdits values in TableView
    By grsandeep85 in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2009, 18:15
  4. how to access widgets values?
    By BalaQT in forum Qt Programming
    Replies: 4
    Last Post: 22nd August 2009, 12:06
  5. inserting string > 127 characters to MS Access
    By jh in forum Qt Programming
    Replies: 0
    Last Post: 12th May 2006, 17:11

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.