2진수를 입렵하려면 어떤 방법을 써야하나요?
조회수 5642회
1 답변
-
GCC에서 - 0b
GCC를 쓰고 있다면 C++14 표준에서는 앞에
0b
를 붙입니다.int x = 0b00010000;
BOOST_BINARY
boost 라이브러리를 쓰려면, itoa() 함수를 써야 합니다. itoa()는 비표준 함수이기 때문에 직접 정의해야 할 수도 있습니다.
#include <boost/utility/binary.hpp> #include <stdio.h> #include <stdlib.h> #include <bitset> #include <iostream> #include <iomanip> using namespace std; int main() { unsigned short b = BOOST_BINARY( 10010 ); char buf[sizeof(b)*8+1]; printf("hex: %04x, dec: %u, oct: %06o, bin: %16s\n", b, b, b, itoa(b, buf, 2)); cout << setfill('0') << "hex: " << hex << setw(4) << b << ", " << "dec: " << dec << b << ", " << "oct: " << oct << setw(6) << b << ", " << "bin: " << bitset< 16 >(b) << endl; return 0; }
출력:
hex: 0012, dec: 18, oct: 000022, bin: 10010 hex: 0012, dec: 18, oct: 000022, bin: 0000000000010010
댓글 입력