[Solved] Converting a string to ASCII
-
Hi there,
I would like to convert a user entered string to ASCII and store the values in an integer, so I can display the result.
I am using the following code...it compiles fine but on_button_clicked() it generates an error...
@ QString input_string = ui->lineEdit->text();
int random;for(int i = 0; i <= input_string.length(); i++) { random = input_string.at(i).toAscii(); } ui->lineEdit_2->setText(QString::number(random));@
Error:
Starting C:\Qt Projects\StringToAscii\debug\StringToAscii.exe...
ASSERT: "i >= 0 && i < size()" in file c:\QtSDK\Desktop\Qt\4.7.4\mingw\include/QtCore/qstring.h, line 702
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
C:\Qt Projects\StringToAscii\debug\StringToAscii.exe exited with code 3Any help would be greatly appreciated.
Thank you in advance.
-
koahnig - isn't post increment a little slower than pre increment? :)
-
-
croussou: You're looping through each character one at a time, and then setting random to be the result of the latest character. It is only after the loop ends that you set your text. At that point it would only contain the last character's value, not a composite of the entire run.
-
[quote author="mlong" date="1330371542"]bq. isn’t post increment a little slower than pre increment? :)
In this case, I don't think it would make a huge difference. :)[/quote]
Well, I agree that it is not a problem in this case. However, it shall be a little slower :-)
I guess for most it is more or less a matter of taste. I personally prefer pre increment and use post increment when I have to.