Because the first byte, byte 0, is the rightmost byte. Basically it is backwards from what you thought, but in reality what you thought was what was backwards.
First of all, one conceptual mistake I'd like to point out:
Endianness you machine uses is determined by hardware architecture. It's not determined by your operating system.
Secondly, the weird output you see on your screen has nothing to do with endianness of your machine. Completely irrelevant.
The real reason is, when you cout something to the stream, it is treated as a char.
Look at your example, you convert temp's pointer to a const char *, and cout the pointer's value. The pointer's value is different from the value of the content that is pointed to (0x58313030, in your case),
although theoretically, they can be the same thing. Imagine the address 0x0x58313030 holds the value 0x58313030.
So you ask, why do I see the strange 001X? Apparently, it's not hex, because we don't have X in hex. Assume the pointer's value is 0x0035fd70. This is a 32 bit address. When the system outputs it on your screen, it interprets the 32 bit binary string as 4 char's. So in our example, the output will be:
0x00 0x35 0xfd and 0x70, in hex.
You look up the table and find the characters represented by those values, and here we go: you see them on the screen.
Thanks. I will look into endiannness part. Somehow, I have thought it is related to OS.
For the second part, in hex, temp = 58 31 30 30, my pointer is 0x58313030.
translated to chars, based on your example, I should see X100, however, my question is why I see 001X on the screen. If endianness is not involved, why right most first?
Humans read text from left to right:
"X100"
If you were to 'add one' to this, you would get "Y100" in your logical mind, right?
Computers read data from right to left:
00000000 00000000 00000000 00000000
If you were to add one to this, you would increment the rightmost bit of the rightmost byte.
The problem is that with your hex you were reading the number backwards, from left to right, when in reality it is read from right to left. You wrote it backwards, so the computer read it backwards. It's as simple as that.
@LB: your math is strange. And how you read text is a cultural matter.
@h9uest: the char pointers have a particular behaviour. Whenn you output them, you are making a memory dump starting from the address pointed, till it find a cell that corresponds to a null character.
I was wrong about some things, but the first byte will always be the first byte, I do not see how changing the order of bytes will change the order the bytes are read - if it changed the order that would be like writing it one way and reading it another.
Sorry. I found I made a mistake. In my previous post:
Secondly, the weird output you see on your screen has nothing to do with endianness of your machine. Completely irrelevant.
It was wrong.
I somehow thought you tried to print on screen the address of the temp.
In fact, you converted the pointer from "long *" to "const char *" and cout it. So you're actually reading the value of temp. Then things get right.
In short, yes, you see the output because your machine uses little endian.
Sorry about the confusion. :p
@ne55:
@h9uest: the char pointers have a particular behaviour. Whenn you output them, you are making a memory dump starting from the address pointed, till it find a cell that corresponds to a null character.
You're right for most of it, except
the char pointers have a particular behaviour.
In fact, it's a particular behaviour of the operator "<<" in ostream. Anyway, thanks for pointing out the rest.