Results 1 to 11 of 11

Thread: Convert QString into QByteArray and vice versa.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2010
    Posts
    72
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Convert QString into QByteArray and vice versa.

    Yes of course it is one of the cases.
    I understand Unicode,Asci,Latin,UTF and I have read the Qt Framework documentation
    I need to convert QString to QByteArray and vice versa so as not to loose any data while converting

    How to accomplish such conversion?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Convert QString into QByteArray and vice versa.

    I don't think you can, from the simple fact, that QByteArray has non visible ASCII values (or can have).
    What you need is not to be able to convert QByteArray to QString, but to apply the functionality you have in QStinrg for your QByteArray data.
    As far as I understand it, you will have to implement that your self.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    Default Re: Convert QString into QByteArray and vice versa.

    Quote Originally Posted by freely View Post
    Yes of course it is one of the cases.
    I understand Unicode,Asci,Latin,UTF and I have read the Qt Framework documentation
    I need to convert QString to QByteArray and vice versa so as not to loose any data while converting

    How to accomplish such conversion?
    But why exactly do you want this conversion to take place? If you want to xor the string then just do it. Each QChar is a 16 bit value, just xor it with 16 bits of your key and you'll get two 8b values that you can store in the byte array.

    Qt Code:
    1. QByteArray xorEncrypt(const QString &string, const QByteArray &key){
    2. QByteArray result;
    3. int keyIndex = 0;
    4. for(int i=0;i<string.size();++i){
    5. quint16 val = string.at(i).unicode();
    6. char high = key.at(keyIndex++);
    7. if(keyIndex==key.size()) keyIndex = 0;
    8. char low = key.at(keyIndex++);
    9. if(keyIndex==key.size()) keyIndex = 0;
    10. quint16 keyPiece = ((high & 0xFF) << 8) | (low & 0xFF));
    11. quint16 encoded = val ^ keyPiece;
    12. result.append(encoded >> 8);
    13. result.append(encoded & 0xFF);
    14. }
    15. return result;
    16. }
    To copy to clipboard, switch view to plain text mode 

    Decoding is similar, you just need to reassemble the string.

    Of course be aware that xor is a very lousy "cipher" that can be broken within a couple of seconds, especially when encoding data consisting mostly of 0x00:
    0x00 xor SECRET = SECRET

    The easiest way to break xor is to feed it with a message (either plaintext or ciphertext) consisting of zeroes. Then the key will reveal itself during encryption/decryption.
    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.


Similar Threads

  1. sending signals to underlying qt code and vice versa
    By technoViking in forum Qt Quick
    Replies: 2
    Last Post: 9th November 2010, 12:10
  2. Mapping JavaScript object to QT (and vice versa)
    By leoalvesmachado in forum Newbie
    Replies: 2
    Last Post: 30th June 2010, 19:00
  3. Replies: 0
    Last Post: 25th March 2010, 12:10
  4. conversion between string to hex and vice versa
    By mohanakrishnan in forum Newbie
    Replies: 2
    Last Post: 5th December 2009, 11:25
  5. Convert QByteArray to object and vice versa
    By DiamonDogX in forum Qt Programming
    Replies: 4
    Last Post: 20th July 2009, 20:07

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.