0

I have a general question about binary numbers in c++. I am reading in a binary file of 32-bit numbers, and then writing these numbers to a text file. My question is, when I do

long int temp;
temp = ( fileBuf[N * 4 * i + 4 * j + 0] << 24 |
         fileBuf[N * 4 * i + 4 * j + 1] << 16 |
         fileBuf[N * 4 * i + 4 * j + 2] << 8  | 
         fileBuf[N * 4 * i + 4 * j + 3] << 0  );
myfile1 << temp << "\t";

does c++ understand that I want it to reinterpret the binary as a decimal number?

Wug
  • 12,956
  • 4
  • 34
  • 54
bigbenbt
  • 367
  • 2
  • 14
  • C++ doesn’t know either about binary nor about decimal numbers, except when reading them from an input stream or writing to an output stream (`iostream`). – Konrad Rudolph Jul 24 '12 at 20:36
  • fileBuf reads in individual bytes, type BYTE. – bigbenbt Jul 24 '12 at 20:46
  • Oh hey, its the rest of the code: http://stackoverflow.com/questions/11527763/byte-flipping-data-in-c-returns-only-zeroes – Wug Jul 24 '12 at 20:47
  • Yeah, I got the rest of it figured out, this was just a check on my part. All my numbers look good, I just wanted to check every last thing before I send this off to another group. – bigbenbt Jul 24 '12 at 20:53

3 Answers3

1

All numbers inside of a C++ int variable are binary. The conversion to or from decimal occurs as the number is read or written, or as the compiler converts it from a constant into code.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
1

If I interpret this question as "Will this do what I want it to?" then the answer is yes, as long as what you want it to do is read a 32 bit, big endian integer out of a buffer (presumably loaded from a file) at offset 4 * N * i + 4 * j.

Assuming of course that fileBuf is declared as a character or unsigned character type. It would behave differently if it were, for example, an array of shorts. You'd end up with a mutilated representation of a 64 bit quantity.

Wug
  • 12,956
  • 4
  • 34
  • 54
0

No. In fact, the number isn't converted to decimal until you print it out in the very last line.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278