Results 1 to 18 of 18

Thread: ASCII Number to Strings & Chars

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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?)

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.