123456789101112131415
string bin2string(unsigned char N) { string sN(8,'0'); unsigned char X; unsigned char Y = 0; for(int i = 0; i < 8; i++){ X = N >> i; if(X == Y) sN[7-i] = '0'; else sN[7-i] = '1'; } cout << sN << endl; return sN; }
1234567
#include <bitset> #include <string> #include <limits> std::string bin2string( unsigned char N ) { return std::bitset< std::numeric_limits<unsigned char>::digits >(N).to_string() ; }
#include <string> #include <limits> std::string bin2string( unsigned char N ) { std::string aN( /*8*/ std::numeric_limits<unsigned char>::digits, '0' ) ; for( std::size_t pos = aN.size() - 1 ; N > 0 ; --pos ) { aN[pos] = N%2 ? '1' : '0' ; N /= 2 ; } return aN ; }