Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Converting a string to ASCII
Forum Updated to NodeBB v4.3 + New Features

[Solved] Converting a string to ASCII

Scheduled Pinned Locked Moved General and Desktop
17 Posts 3 Posters 17.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    croussou
    wrote on last edited by
    #1

    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.

    Regards,

    croussou

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

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

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

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • C Offline
        C Offline
        croussou
        wrote on last edited by
        #3

        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?

        Regards,

        croussou

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on last edited by
          #4

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

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

          1 Reply Last reply
          0
          • ? This user is from outside of this forum
            ? This user is from outside of this forum
            Guest
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • C Offline
              C Offline
              croussou
              wrote on last edited by
              #6

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

              Regards,

              croussou

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mlong
                wrote on last edited by
                #7

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

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

                Software Engineer
                My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mlong
                  wrote on last edited by
                  #8

                  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.

                  Software Engineer
                  My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mlong
                    wrote on last edited by
                    #9

                    Try:
                    @
                    ...
                    QString result;

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

                    Software Engineer
                    My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      croussou
                      wrote on last edited by
                      #10

                      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?

                      Regards,

                      croussou

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        koahnig
                        wrote on last edited by
                        #11

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

                        Vote the answer(s) that helped you to solve your issue(s)

                        1 Reply Last reply
                        0
                        • C Offline
                          C Offline
                          croussou
                          wrote on last edited by
                          #12

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

                          Regards,

                          croussou

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            mlong
                            wrote on last edited by
                            #13

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

                            Software Engineer
                            My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                            1 Reply Last reply
                            0
                            • C Offline
                              C Offline
                              croussou
                              wrote on last edited by
                              #14

                              I would love to, but how to? :S

                              Regards,

                              croussou

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                mlong
                                wrote on last edited by
                                #15

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

                                Software Engineer
                                My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                                1 Reply Last reply
                                0
                                • C Offline
                                  C Offline
                                  croussou
                                  wrote on last edited by
                                  #16

                                  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?

                                  Regards,

                                  croussou

                                  1 Reply Last reply
                                  0
                                  • C Offline
                                    C Offline
                                    croussou
                                    wrote on last edited by
                                    #17

                                    Got it already, thanks anyway!

                                    Regards,

                                    croussou

                                    1 Reply Last reply
                                    0

                                    • Login

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved