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. [Split] QString/QByteArray conversion problems
Forum Updated to NodeBB v4.3 + New Features

[Split] QString/QByteArray conversion problems

Scheduled Pinned Locked Moved General and Desktop
28 Posts 3 Posters 20.5k 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.
  • S Offline
    S Offline
    swhweng
    wrote on last edited by
    #19

    Sorry, ASCII is one to one character to 8 bit conversion in all 0 - 255 range?
    I am not sure
    I need Qt supported codec that converts characters one to one to 8 bit number in 0 -255 range, another worlds each 8 bit number in byte array has it's unique character.
    What is the codec?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      swhweng
      wrote on last edited by
      #20

      Andre I understand - Ok I won't post multiple but by using edit.
      Ok Thanks a Lot. Using hash can I make "unhash" with the same key? How can I get unhashing one - to -one?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #21

        [quote author="Pavel Mazniker" date="1300124063"]Andre I understand - Ok I won't post multiple but by using edit.[/quote]

        You just did.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #22

          [quote author="Pavel Mazniker" date="1300122778"]What codec Qt supports crossplatform that provides one - to - one coding of characters to bytes ( 8 bit numbers ) ?[/quote]

          The whole thing about codecs is to map all the big bunch of non-ASCII characters into some 8-bit representation. This can reduce the mappable characters (like in ISO-8859-1) or it can lead to a representation consisting of multiple 8-bit bytes (like in UTF-8).

          All conversion methods that return QByteArray give you an array of 8bit bits.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • S Offline
            S Offline
            swhweng
            wrote on last edited by
            #23

            [quote author="Volker" date="1300125063"]
            [quote author="Pavel Mazniker" date="1300122778"]What codec Qt supports crossplatform that provides one - to - one coding of characters to bytes ( 8 bit numbers ) ?[/quote]

            The whole thing about codecs is to map all the big bunch of non-ASCII characters into some 8-bit representation. This can reduce the mappable characters (like in ISO-8859-1) or it can lead to a representation consisting of multiple 8-bit bytes (like in UTF-8).

            All conversion methods that return QByteArray give you an array of 8bit bits.[/quote]

            Hi
            Thank you very much for your help.
            Your explanations here at the forum really helped me.
            It seems this time I fixed the problem finally - encryption/decryption works for letters,numbers and @ sign,

            • I've checked for some inputs and it is ok and also theoretically shoud not be any problem( not for any possible character ).So as to be sure 100% one should understand
              how Ascii,Base64 map char to byte.
              I encrypt by manipulating bytes of byte array: QString someString.toAscii(),
              after that I convert it .toBase64() and save it using QDataStream to .dat file.
              When I decrypt I open .dat file with QDataStream,convert readed data from Base64 using QByteArray::fromBase64()
              and make byte manipulations needed for decryption on the byte array decoded from file.
              Pavel
            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #24

              Sounds reasonable.

              One hint: If you use QDataStream you can save yourself the base64 encoding/decoding step, but write and read a QByteArray directly.

              Also I would convert the original string to utf8(), as toAscii() and toLatin1() are conversions that can loose characters (ie. it is a conversion that is not reversible).

              Just try:

              @
              // a cyrillic char and the euro sign
              QString t = QString::fromUtf8("д€");
              qDebug() << "ascii" << t.toAscii().toHex();
              qDebug() << "latin1" << t.toLatin1().toHex();
              qDebug() << "utf8" << t.toUtf8().toHex();
              @

              the output is

              @
              ascii "3f3f"
              latin1 "3f3f"
              utf8 "d0b4e282ac"
              @

              Hex code 3f is question mark; so both chars translate to '?' when using toAscii(), and you will get back to question marks, when re-transforming the bytes to a string.

              And please, please stop thinking in characters when you called any of the toXXX() methods of QString. What you then have is just a bunch of bytes. Treat them as bytes, not as chars!

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • S Offline
                S Offline
                swhweng
                wrote on last edited by
                #25

                I understand UTF8 provides support for wider characters set that Ascii
                So I save QString converted to UTF8 code as byte array ( manipulated ) but when I decrypt I can loose characters once UTF8 is not (one) -to -(one) (character) -to - (8bit number) mapping?

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #26

                  [quote author="Pavel Mazniker" date="1300196573"]I understand UTF8 provides support for wider characters set that Ascii
                  So I save QString converted to UTF8 code as byte array ( manipulated ) but when I decrypt I can loose characters once UTF8 is not (one) -to -(one) (character) -to - (8bit number) mapping?[/quote]

                  No. You can just re-create a QString from your QByteArray containing UTF-8. The operation is completely lossless.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #27

                    The following snippet is guaranteed to work with any value for x (ie. x and y are always equal in the end):

                    @
                    String x = "some fancy string";
                    QByteArray ba = x.toUtf8();
                    String y = QString::fromUtf8(ba);
                    if(x == y)
                    qDebug() << "strings are equal";
                    @

                    If you replace toUtf8() with toAscii() or toLatin1() and replace fromUtf8() with fromAscii() or fromLatin1(), it is not guaranteed that x and y are equal in the end.

                    For some further explanations, please ask Google or your favorite search engine on the topic (text encoding, charsets, etc). That's beyond the topic for a Qt forum.

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #28

                      @Pavel:

                      Perhaps this "Wiki article":http://developer.qt.nokia.com/wiki/Simple_encryption I just published is of use to you? It presents a class I wrote for simple encryption and decryption of QStrings and QByteArrays with a symmetric 64 bit key.

                      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