Related to string to binary conversion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Akshaya Krishna
    New Member
    • Apr 2015
    • 1

    Related to string to binary conversion

    I am doing project on Image steganography. First i read the string. Then, I convert it to binary using below code.
    for(std::size_t i=0;i<str.size( );i++)
    {
    cout<<(bitset<8 >(str[i]));
    }
    the ouput is binary format. But i need to save the binary format of complete string(not each character) to access each bit of the string to hide it in image. HOw to concatenate the multiple bitsets. Please help me....
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Use an array of usigned long.

    You can use bitset::to_ulon g to convert the bitset to an unsigned long integer.

    A bitset<8> will have a value 0-255 so the bits will be in the low end of the unsigned int.

    You should be able to write a function with a unsigned long& as a argument, bitset<8>&, and an enum with a value of OFFSET0, OFFSET8, OFFSET16,etc..

    The function stores the bitset into the unsigned int offset by the value of the enum. It will convert the bitset to an unsigned long using bitset::to_ulon g.

    Since your unsigned int array is guaranteed to be contiguous in memory, your string can be converted into one contiguous array of bits.

    BTW: Instead of using a bitset why bit use str[i] -49 to get the correct bits for the char?

    Comment

    Working...