I have the function that receives a pointer to a string with a name of file to open and to code with 1 and 0; codedLine contains something like 01010011010
After writing to binary file I have exactly the same...
I suppose you want to convert the std::string
input to its binary equivalent.
You can use the std::bitset<> class to convert strings to binary values and vice versa. Writing the string directly to the file results in binary representations of the character values '0'
and '1'
.
An example how to use it:
std::string zeroes_and_ones = "1011100001111010010";
// Define a bitset that can hold sizeof(unsigned long) bits
std::bitset<sizeof(unsigned long) * 8> bits(zeroes_and_ones);
unsigned long binary_value = bits.to_ulong();
// write the binary value to file
codedFile.write((const char*)&binary_value, sizeof(unsigned long));
NOTE
The above sample works with c++11 standards. For earlier version the std::bitset
can't be initialized directly from the string. But it can be filled using the operator>>()
and std::istringstream
for example.