[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 3

    Any help would be greatly appreciated.

    Thank you in advance.


  • Moderators

    @ for(int i = 0; i < input_string.length(); i++)@

    would be better. You are overrunning the end of the string.



  • 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?


  • Moderators

    What is the string that you were giving it, and what is the number that is being given back? What is "the proper integer?"



  • koahnig - isn't post increment a little slower than pre increment? :)



  • 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 :)


  • Moderators

    bq. isn’t post increment a little slower than pre increment? :)

    In this case, I don't think it would make a huge difference. :)


  • Moderators

    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.


  • Moderators

    Try:
    @
    ...
    QString result;

    for(...)
    {
    random = input_string.at(i).toAscii();
    result.append(QString::number(random));
    }
    ui->lineEdit_2->setText(result);
    @



  • 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?


  • Moderators

    [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.



  • [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! :)


  • Moderators

    No problem! Be sure and flag the thread as [Solved]. Thanks!



  • I would love to, but how to? :S


  • Moderators

    Oh sorry. Just edit your original post and add [Solved] to the beginning of the title.



  • 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?



  • Got it already, thanks anyway!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.