c++ - Converting a byte into bit and writing the binary data to file -


suppose have character array, char a[8] containing 10101010. if store data in .txt file, file has 8 bytes size. asking how can convert data binary format , save in file 8 bits (and not 8 bytes) file size 1 byte.

also, once convert these 8 bytes single byte, file format should save output in? .txt or .dat or .bin?

i working on huffman encoding of text files. have converted text format binary, i.e. 0's , 1's, when store output data on file, each digit(1 or 0) takes byte instead of bit. want solution such each digit takes bit.

char buf[100]; void build_code(node n, char *s, int len) { static char *out = buf; if (n->c) {     s[len] = 0;     strcpy(out, s);     code[n->c] = out;     out += len + 1;     return; }  s[len] = '0'; build_code(n->left,  s, len + 1); s[len] = '1'; build_code(n->right, s, len + 1); } 

this how build code tree of huffman tree. ,

void encode(const char *s, char *out) { while (*s)     {     strcpy(out, code[*s]);     out += strlen(code[*s++]);     } } 

this how encode final output.

not entirely sure how end string representing binary representation of value, can integer value string (in base) using standard functions std::strtoul.

that function provides unsigned long value, since know value within 0-255 range can store in unsigned char:

unsigned char v=(unsigned char)(std::strtoul(binary_string_value.c_str(),0,2) & 0xff);

writing disk, can use ofstream write

which file format should save output in? .txt or .dat or .bin?

keep in mind extension (the .txt, .dat or .bin) not mandate format (i.e. structure of contents). extension convention commonly used indicate you're using well-known format (and in os/environments, drives configuration of program can best handle file). since file, define actual format... , name file extension (or no extension) best (or in other words, extension best represent contents) long meaningful , going consume files.

edit: additional details

assuming have buffer of length you're storing string of '0' , '1'

int codesize; // size of code buffer char *code;   // code array/pointer std::ofstream file; // file stream we're writing to.   unsigned char *bytearray=new unsigned char[codesize/8+(codesize%8+=0)?1:0] int bytes=0; for(int i=8;i<codesize;i+=8) {     std::string binstring(code[i-8],8); // create temp string slice of code     bytearray[bytes++]=(unsigned char)(std::strtoul(binstring.c_str(),0,2) & 0xff); }  if(i>codesize) {     // @ point, if there's number of bits not multiple of 8,      // there bits have not     // been writter. not sure how handle it.      // 1 option assume bits 0      // next multiple of 8...  depends on you're representing. }  file.write(bytearray,bytes);  

Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -