[Solved] Converting a string to ASCII
-
wrote on 27 Feb 2012, 19:24 last edited by
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.
-
wrote on 27 Feb 2012, 19:27 last edited by
@ for(int i = 0; i < input_string.length(); i++)@
would be better. You are overrunning the end of the string.
-
wrote on 27 Feb 2012, 19:31 last edited by
Awesome, thanks a bunch.
In addition to that, it seems that the program doesn't provide the proper integer...can you identify what the problem might be?
-
wrote on 27 Feb 2012, 19:35 last edited by
What is the string that you were giving it, and what is the number that is being given back? What is "the proper integer?"
-
wrote on 27 Feb 2012, 19:36 last edited by
koahnig - isn't post increment a little slower than pre increment? :)
-
wrote on 27 Feb 2012, 19:38 last edited by
Okay let me be more specific...
I am giving a single 'A' which is equivalent to 65 (then the output is fine)...now, when I give 'AB' or any other string of characters, I want all the numbers included in the output, i.e. 6566 in this case.
Thanks :)
-
wrote on 27 Feb 2012, 19:39 last edited by
bq. isn’t post increment a little slower than pre increment? :)
In this case, I don't think it would make a huge difference. :)
-
wrote on 27 Feb 2012, 19:44 last edited by
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.
-
wrote on 27 Feb 2012, 19:46 last edited by
Try:
@
...
QString result;for(...)
{
random = input_string.at(i).toAscii();
result.append(QString::number(random));
}
ui->lineEdit_2->setText(result);
@ -
wrote on 27 Feb 2012, 19:46 last edited by
Duh! Sorry, sometimes I just can't get myself around something without a second opinion, hehe.
Thank you for pointing that out! Do I need an int array to fix that? If so, how to set the array the same size as the string?
-
wrote on 27 Feb 2012, 19:46 last edited by
[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.
-
wrote on 27 Feb 2012, 19:50 last edited by
[quote author="mlong" date="1330371973"]Try:
@
...
QString result;for(...)
{
random = input_string.at(i).toAscii();
result.append(QString::number(random));
}
ui->lineEdit_2->setText(result);
@[/quote]Works great, thank you so much! :)
-
wrote on 27 Feb 2012, 19:51 last edited by
No problem! Be sure and flag the thread as [Solved]. Thanks!
-
wrote on 27 Feb 2012, 19:55 last edited by
I would love to, but how to? :S
-
wrote on 27 Feb 2012, 19:56 last edited by
Oh sorry. Just edit your original post and add [Solved] to the beginning of the title.
-
wrote on 27 Feb 2012, 20:04 last edited by
Sure, will do thanks. Just a last thing though, if I may ask, how to make the opposite? Convert back to ASCII that is?
Let's say for example, I have converted A to 65, then add 3 and becomes 68. How to turn 68 to its equivalent ASCII?
-
wrote on 27 Feb 2012, 20:11 last edited by
Got it already, thanks anyway!
1/17