18
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 binary, because in my code, x can be anywhere between 0 and 2^32-1, since

cout<<(int)x;

doesn't do the trick, how would I do it?

2
  • 2
    What does it matter? He posted what he was trying to do so far and why his code wasn't working and asked a clear question. Commented Jul 17, 2010 at 1:09
  • 2
    That's not likely to work well with cout, because you don't have control over how it's opened. If you want to do binary output, open your own stream and include the ios_base::binary flag in your openmmode argument. Commented Jul 17, 2010 at 1:34

6 Answers 6

37

A bit late, but, as Katy shows in her blog, this might be an elegant solution:

#include <bitset>
#include <iostream>

int main(){
  int x=5;
  std::cout<<std::bitset<32>(x)<<std::endl;
}

taken from: https://katyscode.wordpress.com/2012/05/12/printing-numbers-in-binary-format-in-c/

Sign up to request clarification or add additional context in comments.

3 Comments

Probably the most reasonable answer. It does sort of assume that we're talking about a 32-bit int, but we're making such assumptions in almost every other answer too.
@v010dya You can do something like std::bitset<sizeof(int)*8> (or std::bitset<sizeof(decltype(x))*8>) to avoid those assumptions.
I think it's important to note that this answer is not answering the question.
12

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

std::cout.write(reinterpret_cast<const char*>(&x), sizeof x);

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

Comments

4

Try:

int x = 5;
std::cout.write(reinterpret_cast<const char*>(&x),sizeof(x));

Note: That writting data in binary format is non portable.
If you want to read it on an alternative machine you need to either have exactly the same architecture or you need to standardise the format and make sure all machines use the standard format.

If you want to write binary the easiest way to standardise the format is to convert data to network format (there is a set of functions for that htonl() <--> ntohl() etc)

int x = 5;
u_long  transport = htonl(x);
std::cout.write(reinterpret_cast<const char*>(&transport), sizeof(u_long));

But the most transportable format is to just convert to text.

std::cout << x;

6 Comments

where are htonl and ntohl defined? I can't find them in the reference.
@RafaelCamposNunes: Its not part of the C++ standard. Its part of the POSIX standard. linux.die.net/man/3/htonl
Oh, I see. So it's not very portable.
@RafaelCamposNunes It means its not part of the language standard (as the language is hardware agnostic (so it can't be part of that standard)). Its part of the platform standard, as a platform has to know about the hardware details. All modern OS support the POSIX standard. You only get into issues if you are using micro-controllers or something without an OS.
Modern versions of Windows does not support POSIX as far as I know.
|
2

and what about this?

int x = 5;
cout<<(char) ((0xff000000 & x) >> 24);
cout<<(char) ((0x00ff0000 & x) >> 16);
cout<<(char) ((0x0000ff00 & x) >> 8);
cout<<(char) (0x000000ff & x);

1 Comment

Very possibly this is more on the right track than the other answers -- the original question doesn't specify "write in binary" how. If it's "however it happens to be in memory", that's one thing, but if you're trying to be compatible with anything else, then you might want to (for instance) always write a 32-bit long in network byte order, as this answer does (modulo some compatibility issues)
1

A couple of hints.

First, to be between 0 and 2^32 - 1 you'll need an unsigned four-byte int.

Second, the four bytes starting at the address of x (&x) already have the bytes you want.

Does that help?

Comments

1

Starting with C++20, one can also use std::format and the binary format specifier b:

#include <format>
#include <iostream>

int x = 5;
std::cout << std::format("In binary: {:b}\n", x);    // 101
std::cout << std::format("In binary: {:08b}\n", x);  // 00000101
std::cout << std::format("In binary: {:#08b}\n", x); // 0b000101

Godbolt link.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.