1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include<stdio.h>
#include<fstream.h>
// STRUCTURE ALIGNMENT 1-byte set..... ///
struct two_chunk
{
unsigned char chunk1_p1;
unsigned char chunk1_p2:4; // using only 4 bits out of byte.
unsigned char chunk2_p1:4; // using only 4 bits out of byte.
unsigned char chunk2_p2;
};
//to be specific....
typedef unsigned short UINT16;
// function to write binary file.....
void function_to_write(UINT16 chunk1,UINT16 chunk2,fstream fd)
{
struct two_chunk chunks;
chunks.chunk1_p1 = chunk1 & 0x0FF;
chunks.chunk1_p2 = ((chunk1 & 0xF00)>>8);
chunks.chunk2_p1 = chunk2 & 0x00F;
chunks.chunk2_p2 = ((chunk2 & 0xFF0)>>4);
// now write chunks.....
fd.write((char *)&chunks,sizeof(chunks));
}
// main function....
int main()
{
UINT16 your_data_array[12] = {0xAAA,0xBBB,0xAAA,0xBBB,0xAAA,0xBBB,0xAAA,0xBBB,0xAAA,0xBBB,0xAAA,0xBBB}; // say you have 12 pixel information(12 bits each) stored in array.
// open binary file for writing....
fstream binary_file("c:\\test.dat",ios::out|ios::binary|ios::app);
// write data loop
for(int i =0;i<=11;i=i+2) // increment i with two, we are writing two chunks at a time....
{
// write two chunk at a time.....
function_to_write(your_data_array[i],your_data_array[i+1],binary_file); // send two chunk at a time....
}
//close binary file....
binary_file.close();
return 0;
}
|