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. Convert NonAscii ByteArray data to QString
Forum Updated to NodeBB v4.3 + New Features

Convert NonAscii ByteArray data to QString

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.9k Views
  • 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.
  • B Offline
    B Offline
    Beemaneni Bala
    wrote on last edited by
    #1

    Hi Guyz,
    I am confused how to convert non-ascii characters from an byte array.Can anyone pls help me to finish this.

    Here is my code:
    QByteArray data = "0a0a0a0a0aFFFFFFFFFF";
    i want to convert above hex data to integer values. I need to convert 5,6,7 byte to an integer.i.e FFFFFF = 16777215.(respective decimal number)
    How do i do this ? When i read i get "????" as they are non ascii chars.

    Regards
    Bala B

    E matthew.kuiashM 2 Replies Last reply
    0
    • B Beemaneni Bala

      Hi Guyz,
      I am confused how to convert non-ascii characters from an byte array.Can anyone pls help me to finish this.

      Here is my code:
      QByteArray data = "0a0a0a0a0aFFFFFFFFFF";
      i want to convert above hex data to integer values. I need to convert 5,6,7 byte to an integer.i.e FFFFFF = 16777215.(respective decimal number)
      How do i do this ? When i read i get "????" as they are non ascii chars.

      Regards
      Bala B

      E Offline
      E Offline
      Eeli K
      wrote on last edited by
      #2

      @Beemaneni-Bala First you have to know the encoding of the string/characters. Do you know it and what encoding means in general?

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Beemaneni Bala
        wrote on last edited by
        #3

        Not exactly..But i know about utf8 encoding is what QString supports. Am i correct ?

        E 1 Reply Last reply
        0
        • B Beemaneni Bala

          Not exactly..But i know about utf8 encoding is what QString supports. Am i correct ?

          E Offline
          E Offline
          Eeli K
          wrote on last edited by
          #4

          @Beemaneni-Bala So clearly you don't yet know how encoding works. See for example http://kunststube.net/encoding/ and https://en.wikipedia.org/wiki/Character_encoding. If your byte array represents a string, array of characters, it already has encoding implicitly by nature (but the program can't know it). This means you have to have some kind of knowledge what textual character each group of bytes represent. You have to explicitly tell the program what is the encoding because it doesn't yet know it. Sometimes you can know it for sure, sometimes you have to guess.

          See the documentation for QString, search for "encoding" there.

          1 Reply Last reply
          0
          • B Beemaneni Bala

            Hi Guyz,
            I am confused how to convert non-ascii characters from an byte array.Can anyone pls help me to finish this.

            Here is my code:
            QByteArray data = "0a0a0a0a0aFFFFFFFFFF";
            i want to convert above hex data to integer values. I need to convert 5,6,7 byte to an integer.i.e FFFFFF = 16777215.(respective decimal number)
            How do i do this ? When i read i get "????" as they are non ascii chars.

            Regards
            Bala B

            matthew.kuiashM Offline
            matthew.kuiashM Offline
            matthew.kuiash
            wrote on last edited by
            #5

            @Beemaneni-Bala The example seems to show that the data is ASCII formatted hex. If this really is the case then you can use QByteArray data; ... data.mid(5,3).toUint(16);

            If those a raw bytes (they actually contains 0x0a and 0xff) then, for the example shown, you'd do something like int output_int = (((unsigned) (unsigned char) (data.at(5)) << 16) | (((unsigned) (unsigned char) (data.at(5)) << 8) | (unsigned) (unsigned char) (data.at(5));

            You can do much crazier stuff with casting pointers but you'll need to watch out for odd length'd data (you seem to want 24 bit) and byte endianism.

            The legendary cellist Pablo Casals was asked why he continued to practice at age 90. "Because I think I'm making progress," he replied.

            B 1 Reply Last reply
            1
            • matthew.kuiashM matthew.kuiash

              @Beemaneni-Bala The example seems to show that the data is ASCII formatted hex. If this really is the case then you can use QByteArray data; ... data.mid(5,3).toUint(16);

              If those a raw bytes (they actually contains 0x0a and 0xff) then, for the example shown, you'd do something like int output_int = (((unsigned) (unsigned char) (data.at(5)) << 16) | (((unsigned) (unsigned char) (data.at(5)) << 8) | (unsigned) (unsigned char) (data.at(5));

              You can do much crazier stuff with casting pointers but you'll need to watch out for odd length'd data (you seem to want 24 bit) and byte endianism.

              B Offline
              B Offline
              Beemaneni Bala
              wrote on last edited by
              #6

              @matthew.kuiash
              Thank you sir and it worked well...I will get through encoding stuff in mean time

              matthew.kuiashM 1 Reply Last reply
              0
              • B Beemaneni Bala

                @matthew.kuiash
                Thank you sir and it worked well...I will get through encoding stuff in mean time

                matthew.kuiashM Offline
                matthew.kuiashM Offline
                matthew.kuiash
                wrote on last edited by
                #7

                @Beemaneni-Bala Marvellous, if the solves your problem please mark the thread as "Solved".

                The legendary cellist Pablo Casals was asked why he continued to practice at age 90. "Because I think I'm making progress," he replied.

                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