how to output an int in binary?

后端 未结 5 1600
醉梦人生
醉梦人生 2020-12-09 17:41
int x = 5;
cout<<(char)x;

the code above outputs an int x in raw binary, but only 1 byte. what I need it to do is output the x as 4-bytes in

5条回答
  •  再見小時候
    2020-12-09 18:40

    You can use the std::ostream::write() member function:

    std::cout.write(reinterpret_cast(&x), sizeof x);
    

    Note that you would usually want to do this with a stream that has been opened in binary mode.

提交回复
热议问题