Results 1 to 18 of 18

Thread: ASCII Number to Strings & Chars

  1. #1
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Exclamation ASCII Number to Strings & Chars

    I search for help online for a whole day, maybe 2.
    I outputted strings to its ascii value to a file by using the for loops and char .at()

    all these output went well, but how can i read it back from the file?

    i tried qt library and native c++ (not problem in codeblocks, but i want to have gui)

    I'm not sure how to use char array below because I wont know how long the string is, how many chars are there.
    And in my file, its not only 1 line of code. For example, when i input

    ABCD
    AAAA
    BBBB

    my file will become

    65 66 67 68
    65 65 65 65
    66 66 66 66

    Qt Code:
    1. ifstream myFile("abc.txt");
    2.  
    3. while(myFile >> a >> a1 >> a2){
    4.  
    5.  
    6. a = a - 1;
    7. a = a1 - 1;
    8. a= a2 - 1;
    9. QString abc = QChar( a + a1 + a2);
    10. ui->aLine->setText(abc);
    11. }
    To copy to clipboard, switch view to plain text mode 

    when i use just these codes below, it will only shows the first character of the whole strings.
    And is there a way to make the abc still usable outside the loop? cause i need to set it after the loops right? and it always shows not declared.

    Qt Code:
    1. myFile >> a
    2. QString abc = QChar( a );
    3. ui->aLine->setText(abc);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QFile mFile("abc.txt");
    2. mFile.open(QIODevice::ReadOnly | QIODevice::Text);
    3. QDataStream in(&mFile);
    4.  
    5. in >> a; // Not sure what's next. it keeps error
    To copy to clipboard, switch view to plain text mode 
    Last edited by ttimt; 26th November 2012 at 17:16.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: ASCII Number to Strings & Chars

    Your first code snippnet makes no sense, and I cannot see its connection to the stated input and output.

    A QChar is a single character, and a QString is a collection of them. You can access a single character from a QString using QString::at(). You can get the character's Unicode code point from QChar::unicode(). You probably do not need either QString or QChar to do this task.

    QDataStream is not the correct tool to read a text file. See QTextStream

    Generic C++ code that already works stills works in the presence of Qt. The use of Qt in your program does not magically make the rules of C++ disappear. Variables have the same scope in Qt-using programs as they do in any other C++ program.
    Last edited by ChrisW67; 26th November 2012 at 22:55.

  3. #3
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: ASCII Number to Strings & Chars

    well, can u pls show the code?
    my file have numbers, so i can still use qtextstream? and i'm gonna edit the file extension to some random thing.
    I have integers in my file, and when i read it, i want it to convert to chars and to QString. Whenever it meets a new line, it saves it in a new QString.

    And btw, what's the difference of qfile and qiodevice? they seems to have the same function. qiodevice::readonly , qfile::readonly.

    thx
    Last edited by ttimt; 27th November 2012 at 06:45.

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: ASCII Number to Strings & Chars

    my file have numbers, so i can still use qtextstream?
    Yes. why don't you try it...
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #5
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Exclamation Re: ASCII Number to Strings & Chars

    Qt Code:
    1. void reset1::on_pushButton_clicked()
    2. {
    3.  
    4. QFile chk("configp.dll");
    5. chk.open(QFile::ReadOnly);
    6.  
    7. if(chk.exists()){
    8. chk.rename(QString("configp1.dll"));
    9.  
    10. QTextStream in(&chk);
    11. w = in.readLine().;
    12.  
    13.  
    14. ui->label_3->setText(w);
    15.  
    16. }else{
    17. ui->label->setText("Password not Found! Reset Password Fail");
    18. ui->label_3->setText("Press cancel to go back");
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    my program becomes not responding. I solved it by putting 1 in readLine() and it wont show anything.
    Also how do i convert these numbers to char? if i put .fromAscii() behind readline it shows "no matching function for call to 'QString::fromAscii();"

    This is how i input it.

    QFile config("configp.dll");
    config.open(QIODevice::WriteOnly | QIODevice::Text);
    QTextStream out(&config);



    for (int i = 0; i < ui->aLine->text().length(); i++)
    {
    char x =ui->aLine->text().toAscii().at(i);
    out << int(x) << " ";
    }out << endl;

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: ASCII Number to Strings & Chars

    Are you deliberately trying to confuse?

    From your first post, you have a text file containing ASCII text:
    Qt Code:
    1. ABCD
    2. AAAA
    3. BBBB
    To copy to clipboard, switch view to plain text mode 
    you wish to transform into a text file containing more ASCII text:
    Qt Code:
    1. 65 66 67 68
    2. 65 65 65 65
    3. 66 66 66 66
    To copy to clipboard, switch view to plain text mode 
    Your first batch of code does some sort of mangling of a single value based on the three values streamed from the file and doesn't attempt to output anything to a file. In you last post you are renaming an open file, checking whether it exists, all to do something with a password?

    Your original problem is trivially met with:
    Qt Code:
    1. QFile input("input.txt");
    2. QFile output("output.txt");
    3. if (input.open(QIODevice::ReadOnly)) {
    4. if (output.open(QIODevice::WriteOnly)) {
    5. bool needSpace = false;
    6. char c;
    7. while (input.getChar(&c)) {
    8. if (c < ' ') { // new line and other control chars
    9. output.putChar(c);
    10. needSpace = false;
    11. }
    12. else {
    13. if (needSpace)
    14. output.putChar(' ');
    15. output.write( QByteArray::number(c) );
    16. needSpace = true;
    17. }
    18. }
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 
    or, using QTextStream:
    Qt Code:
    1. if (input.open(QIODevice::ReadOnly)) {
    2. if (output.open(QIODevice::WriteOnly)) {
    3. QTextStream in(&input);
    4. QTextStream out(&output);
    5. char c;
    6. bool needSpace = false;
    7. while (!in.atEnd()) {
    8. in >> c;
    9. if (c < ' ') {
    10. out << c;
    11. needSpace = false;
    12. }
    13. else {
    14. out << (needSpace? " ": "") << QByteArray::number(c);
    15. needSpace = true;
    16. }
    17. }
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 
    There are many ways to achieve this.
    Last edited by ChrisW67; 28th November 2012 at 00:39.

  7. #7
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: ASCII Number to Strings & Chars

    absolutely not, the rename file doesn't work so i actually juz put there... :/
    this is the output to file code,no problem with output, just showing the code
    when i enter words on lineedit, it will be saved to numbers in configp.dll
    66 66 66
    67 67 67
    68 68 68

    Qt Code:
    1. QFile config("configp.dll");
    2. config.open(QIODevice::WriteOnly | QIODevice::Text);
    3. QTextStream out(&config);
    4.  
    5. for (int i = 0; i < ui->aLine->text().length(); i++)
    6. {
    7. char x =ui->aLine->text().toAscii().at(i);
    8. out << int(x) << " ";
    9. }
    10. out << endl;
    11.  
    12. for (int i = 0; i < ui->pLine->text().length(); i++)
    13. {
    14. char x =ui->pLine->text().toAscii().at(i);
    15. out << int(x)<< " ";
    16. }
    17. out << endl;
    18.  
    19. for (int i = 0; i < ui->fLine->text().length(); i++)
    20. {
    21. char x =ui->fLine->text().toAscii().at(i);
    22. out << int(x)<< " ";
    23. }
    24. out << endl;
    25.  
    26. for (int i = 0; i < ui->gLine->text().length(); i++)
    27. {
    28. char x =ui->gLine->text().toAscii().at(i);
    29. out << int(x)<< " ";
    30. }
    To copy to clipboard, switch view to plain text mode 

    so how im gonna get string x= aaa , string y = bbb ,string z = ccc which inputted earlier?

    Qt Code:
    1. QFile chk("configp.dll");
    2. chk.open(QIODevice::ReadOnly);
    3. if(chk.exists()){
    4.  
    5. //what should i type here when file exists to get the above (string x, y, z)
    6.  
    7. }else{
    8. ui->label->setText("File not Found!");
    9. ui->label_3->setText("Press cancel to go back");
    10. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ttimt; 28th November 2012 at 04:26.

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: ASCII Number to Strings & Chars

    Right. So, is this the question you wanted to ask?

    I have a file containing three lines like:
    Qt Code:
    1. 65 65 65 66
    2. 66 66 67
    3. 67 68 69
    To copy to clipboard, switch view to plain text mode 
    where each number indicates the ASCII value of a character in decimal. I would like to read this file and produce three strings containing the characters from each line, in the example:
    Qt Code:
    1. AAAB
    2. BBC
    3. CDE
    To copy to clipboard, switch view to plain text mode 
    How can I do this using Qt classes?
    Last edited by ChrisW67; 28th November 2012 at 06:34.

  9. #9
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: ASCII Number to Strings & Chars

    ya,
    string1 = AAAB;
    string2 = bbc
    string3 = cde

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: ASCII Number to Strings & Chars

    Qt Code:
    1. QStringList result;
    2. QFile input("input.txt");
    3. if (input.open(QIODevice::ReadOnly)) {
    4. while (!input.atEnd()) {
    5. QByteArray line = input.readLine().trimmed();
    6. foreach(QByteArray num, line.split(' ')) {
    7. s.append( QChar(num.toShort()) );
    8. }
    9. result << s;
    10. }
    11. }
    12. qDebug() << result;
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: ASCII Number to Strings & Chars

    i want it to appear on a lineedit,
    ui->lineEdit->setText(s);
    ui->lineEdit->setText(result);
    neither works
    error: no matching function for call to 'QLineEdit::setText(QStringList&)'

    and, i'm newbie. do i need to use this codes 3 times? the QStringList is aaab, bbc or cde or all?

    (im using gui, so qDebug() wont work here right?)

  12. #12
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: ASCII Number to Strings & Chars

    lineedit is for showing one string.

    if you want to show string on multiple lines then use a textedit
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  13. #13
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: ASCII Number to Strings & Chars

    i want the first string (AAA) to show on aLine;
    2nd string (BBC) to show on bLine;
    3rd string (CDE) to show on cLine;

  14. #14
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: ASCII Number to Strings & Chars

    so look at QStringList documents and figure out how you can get first string out from it. Then think about the rest
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  15. #15
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: ASCII Number to Strings & Chars

    It is an example, not a cut and paste solution.

    QStringList, as the name implies, is a QList of QStrings. Like any QList you can access one of the elements using QList::at(). Your three strings are the first three elements in the list, i.e. 0, 1, and 2.

    As a newbie you should learn where to find Qt Assistant, or the Help in Qt Creator, and read it.

  16. #16
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: ASCII Number to Strings & Chars

    well, sometimes the help does not provide example its hard to understand.

    Qt Code:
    1. QStringList result;
    2. QFile input("configp.dll");
    3. if (input.open(QIODevice::ReadOnly)) {
    4. while (!input.atEnd()) {
    5. QByteArray line = input.readLine().trimmed();
    6.  
    7. foreach(QByteArray num, line.split(' ')) {
    8. s.append( QChar(num.toShort()) );
    9.  
    10. }
    11. result << s;
    12. ui->aLine->setText(result::at(1));
    13. }
    14. }else
    To copy to clipboard, switch view to plain text mode 
    runtime error

    if changed to
    ui->aLine->setText(result.QList::at(1));
    ERROR : template<class T> class QList' used without template parameters
    Last edited by ttimt; 28th November 2012 at 08:25.

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: ASCII Number to Strings & Chars

    Quote Originally Posted by ttimt View Post
    well, sometimes the help does not provide example its hard to understand.
    The help is not an almanach of examples, it's a means to learn how things work by reading it and applying the knowledge gained in practise in your own code.

    The error you got is a C++ error, Qt help has nothing to do with this. When you get that fixed, analyze your code and think what will be the ultimate result of your code. I'll simplify it for you so it's easier to read:

    Qt Code:
    1. QStringList result;
    2. result << "something" << "somethingelse";
    3. while(!input.atEnd()) {
    4. ui->aLine->setText(result.at(1));
    5. }
    To copy to clipboard, switch view to plain text mode 

    Please focus on subsequent iterations of the while loop.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. #18
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: ASCII Number to Strings & Chars

    hmmm.... it seems the problem is QStringList results have nothing in it? Hopefully im right
    And when i use canReadLine(), the program actually outputted cant read line

    Qt Code:
    1. QStringList result;
    2. result << "Something" << "abc";
    3. QFile input("configp.dll");
    4.  
    5. if (input.open(QIODevice::ReadOnly )) {
    6.  
    7. if(input.canReadLine()){
    8.  
    9. ui->hLine->setText("can read");
    10.  
    11. }else{
    12.  
    13. //EDITED : removed this line QApplication::processEvents();
    14. ui->hLine->setText("cant read line"); //outputted this " can't read line "
    15. }
    16.  
    17. while (!input.atEnd()){
    18.  
    19. QByteArray line = input.readLine().simplified(); //trimmed to simplified
    20. QString foo; //actually which line of code chris provided converted ascii numbers to char?
    21.  
    22. foreach(QByteArray num, line.split(' ')) {
    23. foo.append( QChar(num.toShort()) ); // is it this .toShort?
    24. }
    25. result << foo;// if use line->setText() here also output same..
    26. }
    27. ui->aLine->setText(result.at(0)); // this output Something
    28.  
    29. ui->fLine->setText(result.at(1)); //this output abc not anything from the file
    To copy to clipboard, switch view to plain text mode 


    Edited : if i add these code below, it actually shows "can read"

    Qt Code:
    1. if (input.open(QIODevice::ReadOnly )) {
    2. char c;
    3. input.getChar(&c);
    4. if(input.canReadLine()){
    5. ui->hLine->setText("can read");
    6. }else{
    7. ui->hLine->setText("cant read line");
    8. }
    To copy to clipboard, switch view to plain text mode 

    EDITED: AND HEY, AFTER HOURS OF CODING, I MANGED TO LET IT shows up on aLine But (didn't know i put it inside while loop)after showing up, it still goes runtime error, so i guess i gonna figure out what wysota(hope i spell it correct) is saying

    EDITED: I actually found i duplicated settext that caused error. it's working now but figuring out how to output only available line. By the way, will moderator ban for too many edits? I still have some problems with rename. qdir wont work too

    EDITED: it now shows runtime error again.
    EDITED: i actually jumped setText() from 0 to 3 where i only wanna jump 0 to 2, NOTE the rename still doesn't work

    Qt Code:
    1. QFile chk("configp.dll");
    2. chk.open(QIODevice::ReadOnly);
    3. if(chk.exists()){
    4.  
    5. QFile exs("configp1.dll");
    6. if(exs.open(QIODevice::ReadOnly))
    7. exs.remove(); //this can delete, works very well
    8.  
    9.  
    10. chk.rename("configp1.dll"); // it wont rename
    11.  
    12. }else{
    To copy to clipboard, switch view to plain text mode 
    Last edited by ttimt; 28th November 2012 at 17:26.

Similar Threads

  1. qgetenv and special chars
    By miraks in forum Qt Programming
    Replies: 8
    Last Post: 24th October 2020, 01:05
  2. Replies: 1
    Last Post: 5th March 2012, 06:34
  3. QTextEdit 80 chars wide
    By mpi in forum Qt Programming
    Replies: 4
    Last Post: 29th January 2011, 13:18
  4. Static casting of signed chars
    By ShamusVW in forum Qt Programming
    Replies: 0
    Last Post: 4th November 2009, 05:46
  5. Need help. can't append chars in QString
    By AcerExtensa in forum Qt Programming
    Replies: 6
    Last Post: 12th June 2008, 10:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.