Results 1 to 9 of 9

Thread: Base64 - cannot decode properly

  1. #1
    Join Date
    Aug 2015
    Location
    Poland/UK
    Posts
    30
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Base64 - cannot decode properly

    Hi, I've got a problem I don't know whats going on, it was working well but now it doesn't.
    I want to decode a base64 binary file from QString or QByteArray and then save it, but QByteArray::fromBase64 reads only one (first) line of the file...
    Could somebody tell me why cant read all and then decode it ?

    I checked using some web decoder and it is encoded properly so It shouldn't cause any problems.

    Encoded file: http://wklej.org/id/2269494/txt/

    http://4img.eu/images/2016/04/11/Scr...4-11201728.png

    Qt Code:
    1. void MainWindow::on_btnDecode_clicked()
    2. {
    3. QString input = ui->inputText->toPlainText();
    4.  
    5. QString output = QByteArray::fromBase64(input.toLatin1());
    6. ui->outputText->setText(output);
    7. }
    To copy to clipboard, switch view to plain text mode 

    The code is very simple as You can see, this is only test program to help solve decoding problem in my app.

    Thank You for any help
    Cheers,

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Base64 - cannot decode properly

    How are you reading "inputText" to get "input"? From the text you posted, it looks like this is EOL delimited, so there is a good chance that interpretation is stopping at the end of the first line. Your online decoder is probably smarter than that and is stripping EOL characters from the string before interpreting it.

  3. #3
    Join Date
    Aug 2015
    Location
    Poland/UK
    Posts
    30
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Base64 - cannot decode properly

    I know that something is wrong with EOL, but I can't remove it, I tried exactly everything

    eg

    input.remove(QRegExp("[\\n\\t\\r]"));

    and

    input.simplified();

    plus many others...
    and still doesn't work

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Base64 - cannot decode properly

    When you execute this line:

    Qt Code:
    1. QString input = ui->inputText->toPlainText();
    To copy to clipboard, switch view to plain text mode 

    What does "input" contain? Is it everything, or just the first line?

  5. #5
    Join Date
    Aug 2015
    Location
    Poland/UK
    Posts
    30
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Base64 - cannot decode properly

    It contains everything I just pasted all text from http://wklej.org/id/2269494/txt/ into QTextEdit

    The file is stream from server response (IMAP Client)

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Base64 - cannot decode properly

    I guess the problem is that I don't know if what you have pasted into the "wklej.org" web site is *exactly* that you get from your IMAP client, or if if has been mangled for display by the web site (or by QTextEdit). The lines I copy from the web site definitely have "CRLF" on the end of every line, so doing this:

    Qt Code:
    1. QString input = ui->inputText->toPlainText().simplified();
    To copy to clipboard, switch view to plain text mode 

    should get rid of them and give you one giant string with no whitespace.

  7. #7
    Join Date
    Aug 2015
    Location
    Poland/UK
    Posts
    30
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Base64 - cannot decode properly

    I know what You mean, but it doesn't work for some reason...

    Have a look: http://www56.zippyshare.com/v/znevGrDH/file.html
    This is a very simple example, just compile it and paste this: http://wklej.org/id/2269494/txt/
    Still reads only one line

    Cheers,

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Base64 - cannot decode properly

    Replace your code with this:

    Qt Code:
    1. void MainWindow::on_btDecode_clicked()
    2. {
    3. QString input = ui->inputText->toPlainText().simplified();
    4.  
    5. QByteArray latin1 = input.toLatin1();
    6. QByteArray outputBytes = QByteArray::fromBase64( latin1 );
    7. QString output = outputBytes;
    8. ui->outputText->setText(output);
    9. }
    To copy to clipboard, switch view to plain text mode 

    In the debugger, if you set a breakpoint after QByteArray:: fromBase64() and look at the contents of "outputBytes" you will see that it contains the entire decoded string. Unfortunately, the byte array contains embedded '\0' characters, which are being interpreted as string terminators. So when you copy "outputBytes" to "output", it stops when it gets to the first '\0', and that's what you see.

    I don't know what this is that you are trying to decode, but it looks like a mixture of ASCII and binary, and from some of the text I read I suspect you're trying to decompile a program. That's suspicious behaviour as far as I'm concerned, so that will be the end of my help unless you can come up with a good explanation.

  9. #9
    Join Date
    Aug 2015
    Location
    Poland/UK
    Posts
    30
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Base64 - cannot decode properly

    Thank You very much, yeah it contains a binary data, I "splitted" some program installator I had in my Downloads folder by WinRar to get 1MB files, I don't want to decompile anything, on the other hand it could be extremely difficult taking C++ app

Similar Threads

  1. Decode base64 into PDF file
    By nhocjerry in forum Qt Programming
    Replies: 3
    Last Post: 31st July 2013, 06:08
  2. Encode/decode base 64 images
    By fitzy in forum Qt Programming
    Replies: 6
    Last Post: 28th October 2009, 19:38
  3. How can i decode binary base64
    By Askar in forum Qt Programming
    Replies: 1
    Last Post: 14th September 2009, 04:01
  4. QT Class to decode PDU data
    By cutie.monkey in forum Newbie
    Replies: 0
    Last Post: 4th July 2009, 06:52
  5. To decode .ithmb files.
    By merry in forum Qt Programming
    Replies: 2
    Last Post: 10th January 2008, 08:17

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.