Results 1 to 12 of 12

Thread: Read 12-bits values from file

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,327
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Read 12-bits values from file

    why do you create a vector of 4 bytes?
    I see I made a mistake - somehow I confused myself into thinking a byte was 4 bits. Sorry. What I proposed will not work at all for your problem.

    You need to read the file in 24-bit chunks - three 8-bit bytes at a time - to match two 12-bit "words". So read your data into an unsigned char [ 3 ] array. You then chop this into two 16-bit integers by taking all of the first byte and the first half of the second byte and putting it into one integer, then the second half of the second byte and all of the third byte and putting it into the second integer.

    Use a union like this:

    Qt Code:
    1. unsigned char inData[ 3 ];
    2.  
    3. union SplitData
    4. {
    5. unsigned char bytes[ 2 ];
    6. uint_16 intVal;
    7. }
    8.  
    9. // Need to check for end of file, just in case there is an odd number of 12-bit values.
    10. // In that case, don't read the third byte.
    11. input >> inData[ 0 ] >> inData[ 1 ] >> inData[ 3 ];
    12.  
    13. uint_16 value1;
    14. uint_16 value2;
    15.  
    16. SplitData splitData;
    17.  
    18.  
    19. splitData.bytes[ 0 ] = inData[ 0 ];
    20. splitData.bytes[ 1 ] = inData[ 1 ] & 0xF0; // take first four bits
    21.  
    22. // Might have to do this bit shifting instead of just straight assignment
    23. value1 = splitData.intVal >> 4;
    24.  
    25. splitData.bytes[ 0 ] = inData[ 1 ] & 0x0F; // take second four bits
    26. splitData.bytes[ 1 ] = inData[ 2 ];
    27.  
    28. // Here it is OK because the first 4 bits have already been masked off to zero
    29. value2 = splitData.intVal;
    To copy to clipboard, switch view to plain text mode 

    If there is an odd number of 12-bit values in the file, then ignore value2 for the last read.

    You might have to swap bytes and masking around because of endianess, but the basic idea is the same.
    Last edited by d_stranz; 23rd September 2022 at 16:49.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Mixing MySQL 32 and 64 bits with Qt 32 and 64 bits
    By ^NyAw^ in forum Installation and Deployment
    Replies: 4
    Last Post: 11th December 2014, 17:21
  2. Replies: 11
    Last Post: 28th June 2012, 11:17
  3. Replies: 5
    Last Post: 26th June 2012, 07:39
  4. How to read and get the values out of the xml?
    By dark1988 in forum Qt Programming
    Replies: 1
    Last Post: 26th July 2008, 00:29
  5. How to read and get the values out of the xml?
    By dark1988 in forum Qt Programming
    Replies: 6
    Last Post: 15th July 2008, 08:41

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.