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:
unsigned char inData[ 3 ];
union SplitData
{
unsigned char bytes[ 2 ];
uint_16 intVal;
}
// Need to check for end of file, just in case there is an odd number of 12-bit values.
// In that case, don't read the third byte.
input >> inData[ 0 ] >> inData[ 1 ] >> inData[ 3 ];
uint_16 value1;
uint_16 value2;
SplitData splitData;
splitData.bytes[ 0 ] = inData[ 0 ];
splitData.bytes[ 1 ] = inData[ 1 ] & 0xF0; // take first four bits
// Might have to do this bit shifting instead of just straight assignment
value1 = splitData.intVal >> 4;
splitData.bytes[ 0 ] = inData[ 1 ] & 0x0F; // take second four bits
splitData.bytes[ 1 ] = inData[ 2 ];
// Here it is OK because the first 4 bits have already been masked off to zero
value2 = splitData.intVal;
unsigned char inData[ 3 ];
union SplitData
{
unsigned char bytes[ 2 ];
uint_16 intVal;
}
// Need to check for end of file, just in case there is an odd number of 12-bit values.
// In that case, don't read the third byte.
input >> inData[ 0 ] >> inData[ 1 ] >> inData[ 3 ];
uint_16 value1;
uint_16 value2;
SplitData splitData;
splitData.bytes[ 0 ] = inData[ 0 ];
splitData.bytes[ 1 ] = inData[ 1 ] & 0xF0; // take first four bits
// Might have to do this bit shifting instead of just straight assignment
value1 = splitData.intVal >> 4;
splitData.bytes[ 0 ] = inData[ 1 ] & 0x0F; // take second four bits
splitData.bytes[ 1 ] = inData[ 2 ];
// Here it is OK because the first 4 bits have already been masked off to zero
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.
Bookmarks