How can I convert hexadecimal numbers to binary in C++?

后端 未结 6 427
星月不相逢
星月不相逢 2020-12-05 06:08

I am taking a beginning C++ class, and would like to convert letters between hex representations and binary. I can manage to print out the hex numbers using:



        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 06:29

    Like so:

    for(char c = 'a'; c <= 'z'; c++){
            std::bitset binary(c); //sizeof() returns bytes, not bits!
            std::cout << "Letter: " << c << "\t";
            std::cout << "Hex: " << std::hex << (int)c << "\t";
            std::cout << "Binary: " << binary << std::endl;
        }
    

提交回复
热议问题