Results 1 to 12 of 12

Thread: Read 12-bits values from file

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    681
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Read 12-bits values from file

    Good morning,
    I have to read values from an input file where each number represents a 12-bits value.
    How can I do this in C++?
    I created a
    Qt Code:
    1. struct value_struct
    2. {
    3. unsigned value: 12;
    4. };
    To copy to clipboard, switch view to plain text mode 

    Then I used ifstream to read the value from the file
    Qt Code:
    1. value_struct data;
    2. ifstream input;
    3. input.open("in.txt");
    4. if (!input)
    5. {
    6. //show error
    7. }
    8. input >> data.value; //gives a compiler error
    To copy to clipboard, switch view to plain text mode 
    Franco Amato

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,164
    Thanks
    296
    Thanked 856 Times in 843 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Read 12-bits values from file

    The error is because ifstream has no operator>>() that can handle a 12-bit value.

    Are these 12-bit values packed in the file? That is, is the file 12bits-12bits-12bits, or are the 12 bits the first 12 of a 16-bit "frame"? And is the value little or big endian?

    If the 12-bit values are packed, then you will probably have to read them one unsigned char at a time, then construct an integer from that.

    Qt Code:
    1. union value_struct
    2. {
    3. unsigned char charVal[ 4 ];
    4. uint_16 intVal;
    5. };
    6.  
    7. value_struct data;
    8. data.intVal = 0; // Ensures all bytes contain zero
    9. input >> data.charVal[0] >> data.charVal[1] >> data.charVal[2];
    10.  
    11. // use data.intVal ...
    To copy to clipboard, switch view to plain text mode 

    If there are endian issues, you may need to swap the order in which you read in the bytes so that when you access the integer value, it is a correct number.

    Don't worry about the efficiency in reading a byte at a time. The OS will probably read a large chunk if not the entire file into an in-memory buffer and access it from there, not read it a byte at a time from disk.
    Last edited by d_stranz; 22nd September 2022 at 21:15.
    <=== 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.

  3. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    681
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read 12-bits values from file

    Hi,
    the file contains 12bits 12bits 12bits ... little endian.
    I didn't get your example, why do you create a vector of 4 bytes? And how to use data.intVal if it contains only 0?
    Franco Amato

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,164
    Thanks
    296
    Thanked 856 Times in 843 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 17: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.

  5. #5
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    681
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read 12-bits values from file

    I am still not understanding.
    Why did you write:
    Qt Code:
    1. input >> inData[ 0 ] >> inData[ 1 ] >> inData[ 3 ];
    To copy to clipboard, switch view to plain text mode 

    and not
    Qt Code:
    1. input >> inData[ 0 ] >> inData[ 1 ] >> inData[ 2 ];
    To copy to clipboard, switch view to plain text mode 

    And why are you reading 3 elements at time? In my file I have rows of 64 items. Each row is separated by new line character
    Franco Amato

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,164
    Thanks
    296
    Thanked 856 Times in 843 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Read 12-bits values from file

    Why did you write:
    Sorry again, it is a typo. Should be inData[ 2 ] as you said.

    And why are you reading 3 elements at time? In my file I have rows of 64 items. Each row is separated by new line character
    You said you had 12 bit integers in your file, so I assumed that the file is binary. Now you are seeming to say that the file is actually a text file, since it has newlines in it. If it is a text file, then how can it contain 12-bit integers? And what is an "item" - a 12 bit integer or something else?

    What EXACTLY is the format of these files? Text, binary, what? If it is binary, then is it 12-bit integers, packed 2 integers to every 3 bytes (which it would be if the format is 12 bits - 12 bits - 12 bits, like you said - every three 8-bit bytes holds two 12-bit numbers), or is it something else? The code I wrote takes every 3 bytes and unpacks them into two integers, 1 1/2 bytes (12 bits) into one, 1 1/2 bytes into the next.

    If it is a text file, then copy and paste a few lines into your reply so we can see what it looks like.
    <=== 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, 18:21
  2. Replies: 11
    Last Post: 28th June 2012, 12:17
  3. Replies: 5
    Last Post: 26th June 2012, 08: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, 01: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, 09: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.