Results 1 to 5 of 5

Thread: Program Crash HOW TO get user input and convert to hexadecimal

  1. #1
    Join Date
    Jul 2009
    Posts
    3
    Thanks
    2

    Default Program Crash HOW TO get user input and convert to hexadecimal

    Hi Dear All, I am writing a program which will read the user's input from lineEdit and convert it to Hexadecimal, but seems the program will crash, and my Fedora 11 starts to collect bugs.


    void MainWindow:n_changeMACButton_clicked()
    {

    QLineEdit *lineEdit = new QLineEdit(this);
    QString hex[6]=NULL;
    for(int i=0;i<6;i++){
    hex]=lineEdit->text();}
    }

    After i build(sucessful) and run it, the whole program will crash and Fedora will start collecting bugs. Think quite some time but still can not get any clue.......please help

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Program Crash HOW TO get user input and convert to hexadecimal

    What kind of input do you expect? Do you wish to convert each character to it's number representation or do you wish to read a number and display that in hexadecimal format?

    I'm pretty sure your code doesn't do what you want it to.

    Qt Code:
    1. QString hex[6]=NULL;
    To copy to clipboard, switch view to plain text mode 
    This is an array of 6 QStrings. You can't initialize it to NULL.
    Last edited by franz; 23rd July 2009 at 22:34.

  3. The following user says thank you to franz for this useful post:

    hbtdtg (24th July 2009)

  4. #3
    Join Date
    Jul 2009
    Posts
    13
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Program Crash HOW TO get user input and convert to hexadecimal

    the QLineEdit should not be a local variable -- it should be on the form. Suggest you use something like this to get the hex value:


    Qt Code:
    1. bool ok;
    2. int num = lineEdit.text().toInt(&ok);
    3. QString hexRep = QString::number(num,16);
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to tjm for this useful post:

    hbtdtg (24th July 2009)

  6. #4
    Join Date
    Jul 2009
    Posts
    3
    Thanks
    2

    Default Re: Program Crash HOW TO get user input and convert to hexadecimal

    Thanks guys, I already solved the problem. your suggestions gave alot hint to me....

    I just did like this

    QString hex[6];
    const char *set;
    int hexnum[6];
    / for loop/
    hex[i]=ui->lineEdit-text();


    /for loop/
    hexnum[i]=(char) (strtoul (hex[i].toAscii().data(),0,16) &0xFF);

    Thanks

  7. #5
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    503
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Program Crash HOW TO get user input and convert to hexadecimal

    Quote Originally Posted by hbtdtg View Post
    /for loop/
    hexnum[i]=(char) (strtoul (hex[i].toAscii().data(),0,16) &0xFF);
    Hi, instead of this construction, you could use QString::toInt to convert your string to a hex number.

    Ginsengelf

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.