I will just post this as an answer. It is shorter, safer and, most importantly, done .
#include <string>
#include <bitset>
#include <type_traits>
#define IS_INTEGRAL(T) typename std::enable_if< std::is_integral<T>::value >::type* = 0
template<class T>
std::string integral_to_binary_string(T byte, IS_INTEGRAL(T))
{
std::bitset<sizeof(T) * CHAR_BIT> bs(byte);
return bs.to_string();
}
int main(){
unsigned char byte = 0x03;
std::cout << integral_to_binary_string(byte);
std::cin.get();
}
Conclusion:
00000011
The function name has been changed, although I'm not happy with this ... does anyone have a good idea?
source
share