Results 1 to 9 of 9

Thread: How come this doesnt work?

  1. #1
    Join Date
    Apr 2007
    Posts
    62
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How come this doesnt work?

    I wrote some code that tries to extract an integer 100 out of this string:

    "AB,SH#100 \r\n\0"

    but the toInt() always fails; i.e. 0 is returned from toInt(). Note that the original data came in from QByteArray(). I currently use append() to convert from QByteArray to QString.

    As a separate question, is there a better way to convert QByteArray to QString than QString::append()?


    Qt Code:
    1. QByteArray bte = "AB,SH#100 \r\n\0";
    2. QString test;
    3. test.append( bte );
    4. QStringList list = test.split( "," );
    5. QStringList tmp = list.filter( "SH#" );
    6.  
    7. tmp[0].remove( 0, 3 );
    8. int value = test.toInt();
    To copy to clipboard, switch view to plain text mode 

    Thanks!

  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: How come this doesnt work?

    If you remove the three characters from the beginning, you still have the whitespaces and 0 at the end, so toInt() will fail.

  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: How come this doesnt work?

    Why not just use QRegExp?
    ==========================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
    Apr 2007
    Posts
    62
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How come this doesnt work?

    can somebody post the solution in code?

  5. #5
    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: How come this doesnt work?

    What is exactly that you want? You have to describe a general case and not a single example string.

  6. #6
    Join Date
    Apr 2007
    Posts
    62
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How come this doesnt work?

    [Quote]
    I wrote some code that tries to extract an integer 100 out of this string:

    "AB,SH#100 \r\n\0"
    [/quoute]

    That's exactly what I want. Just try extracting it. Note to wyota: toInt() doesnt fail even if you have 100 spaces after SH#100

  7. #7
    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: How come this doesnt work?

    Quote Originally Posted by ShaChris23 View Post
    I wrote some code that tries to extract an integer 100 out of this string:

    "AB,SH#100 \r\n\0"
    That's exactly what I want. Just try extracting it.[/QUOTE]

    Qt Code:
    1. QString func(const QString &s){ return "100"; }
    To copy to clipboard, switch view to plain text mode 
    I doubt that's what you want...

    If you don't tell us what are the rules of extraction in general way, we can't possibly give you code (other than I have given above) to do it. You said "extract 100", so return "100" seems a fine way to do it.

  8. #8
    Join Date
    Apr 2007
    Posts
    62
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How come this doesnt work?

    Hi,

    Thanks for all the inputs. How about this:

    A string contains many fields. Each field starts with a 2 ASCII characters followed by the # sign.

    Each field is delimited by a comma.

    The last field is terminated by "\r\n"

    We need to extract the number ( int or float ) after the # sign.

    e.g. "DV#1200.03,AB#34,SH#100\r\n\0"

    So for example, if a user is interested in the field "SH", he has to

    Extract the SH field out,
    Chop off first 3 characters
    Convert the remaining string to be a number.

    I hope this is helpful. I might be wrong here...but I think there's a bug in the Qt's QstringList::filter() method.

    Thanks!

  9. #9
    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: How come this doesnt work?

    I'd do:
    Qt Code:
    1. float extractCmd(const QString &str, const QString &expected){
    2. if(expected.size()>2 || str.size()<4) return 0;
    3. QStringList commands = str.simplified().split(",");
    4. static QRegExp rx("([A-Z][A-Z])#([0-9]*\\.?[0-9]+)");
    5. foreach(QString cmd, commands){
    6. if(!rx.exactMatch(cmd))
    7. continue;
    8. if(rx.cap(1)==expected)
    9. return rx.cap(2).toFloat();
    10. }
    11. }
    12.  
    13. QString str = "DV#1200.03,AB#34,SH#100\r\n\0";
    14. float val = extractCmd(str, "SH");
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QWidget -> updateGeometry dont work.
    By patrik08 in forum Qt Programming
    Replies: 7
    Last Post: 22nd May 2013, 16:55
  2. Change work area OS
    By pakulo in forum Qt Programming
    Replies: 15
    Last Post: 15th May 2007, 08:20
  3. Work offline and Archive web page in Konqueror
    By jamadagni in forum KDE Forum
    Replies: 0
    Last Post: 22nd February 2006, 10:18
  4. QTextEdit Justify align making work
    By dec0ding in forum Qt Programming
    Replies: 2
    Last Post: 13th January 2006, 13:02

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.