PDA

View Full Version : Program Crash HOW TO get user input and convert to hexadecimal



hbtdtg
23rd July 2009, 19:33
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::on_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

franz
23rd July 2009, 21:29
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.



QString hex[6]=NULL;

This is an array of 6 QStrings. You can't initialize it to NULL.

tjm
23rd July 2009, 21:31
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:



bool ok;
int num = lineEdit.text().toInt(&ok);
QString hexRep = QString::number(num,16);

hbtdtg
24th July 2009, 08:16
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

Ginsengelf
24th July 2009, 08:48
/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