Friday, December 2, 2016

C++ function to convert int to it's binary representation in string

std::string convertInt2Binary(int pi_int)
{
    std::string s;
    int n = std::numeric_limits<int>::digits - 1;

    s.reserve(n + 1);

    do
        s.push_back(((pi_int >> n) & 1) + '0');
    while(--n > -1);

    return s;
}