Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Char array to qint24 array
QtWS25 Last Chance

Char array to qint24 array

Scheduled Pinned Locked Moved C++ Gurus
5 Posts 4 Posters 3.1k 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.
  • G Offline
    G Offline
    goocreations
    wrote on last edited by
    #1

    I'm doing music processing on 24bit samples. The raw samples are stored in a char array. I want to convert this array to a qint24 array.

    Now since there is no qint24 type, I decided to use qint32. A char has a size of 1 byte, so I will have to convert 3 chars to a 24bit sample at a time and store it in a qint32. This is what I'm currently doing:

    Code:

    @char temp[3];
    int bufferCurrent;
    for(int i = 0; i < size; ++i)
    {
    bufferCurrent = i * 3;
    temp[0] = buffer[bufferCurrent];
    temp[1] = buffer[bufferCurrent + 1];
    temp[2] = buffer[bufferCurrent + 2];
    result[i] = reinterpret_cast<qint32>(temp);
    }@

    buffer is the char array and result my new array for the 24bit samples. g++ complaints that it can't convert char* to qint32 (loses precision).

    So I basically need some way to convert 3 chars to a qint32. Any ideas of how to accomplish this?

    1 Reply Last reply
    0
    • F Offline
      F Offline
      favoritas37
      wrote on last edited by
      #2

      You could try playing with shifts
      @
      char temp[3];
      int bufferCurrent = 0;
      for(int i = 0; i < size; i++)
      {
      int intermediate = (int)buffer[bufferCurrent];
      intermediate |= ((int)buffer[bufferCurrent + 1]) << 8;
      intermediate |= ((int)buffer[bufferCurrent + 2]) << 16;
      result[i] = intermediate;
      bufferCurrent += 3;
      }
      @

      or the following depending on the endianness of the message:

      @
      char temp[3];
      int bufferCurrent = 0;
      for(int i = 0; i < size; i++)
      {
      int intermediate = (int)buffer[bufferCurrent+2];
      intermediate |= ((int)buffer[bufferCurrent + 1]) << 8;
      intermediate |= ((int)buffer[bufferCurrent]) << 16;
      result[i] = intermediate;
      bufferCurrent += 3;
      }
      @

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goocreations
        wrote on last edited by
        #3

        Thanks. I assume << 8 and << 16 determines where the corresponding value will be placed in bytes (eg: starting at bit 8 or bit 16).

        1 Reply Last reply
        0
        • W Offline
          W Offline
          Wilk
          wrote on last edited by
          #4

          Hello
          In fact << 8 and << 16 mean "bit shifts":http://en.wikipedia.org/wiki/Bitwise_operation#Bit_shifts.

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

            If you want to reinterpret to a 32 bit type, you need 32bits in the beginning. So temp should be an array of four bytes with the high byte set to 0, not 3 bytes (24 bits).

            Also, be aware of endianess issues - it depends on the CPU architecture whether the most significant byte is at index 0 or at index 3.

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

            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