C++ Builder: Convert binary code to AnsiString

841 views Asked by At

when I have binary code like '01010100011001010111001101110100", how can I convert this back to "test"?

Thanks.

2

There are 2 answers

0
George Houpis On
#include <iostream>
#include <string>
#include <climits>

int main( int, char ** )
{
   std::string str( "01010100011001010111001101110100" );

   std::string result;
   for( size_t i = 0; i < ( str.size() / CHAR_BIT ); ++i )
      result.push_back( static_cast< char >( stoi( str.substr( 8 * i, CHAR_BIT ), nullptr, 2 ) ) );

   std::cout << "result = " << result << std::endl;

   return 0;
}
0
user2899525 On

There is many ways to get this. It depends on the language. In general, you would break down the binary string in chunks of 8 bits (byte). Then you would calculate the decimal value of each byte. Link below goes over how to do that. Once you have the decimal value, you have each letter. From here, it depends on the language. In C you could make a char array of each and make a c-string.

http://www.wikihow.com/Convert-from-Binary-to-Decimal

You could also do a look up table.