i have big and little endian problem. I'm using a little endian system.
//C++ Code
#include <iostream>
#include <arpa/inet.h>
using namespace std;
int main(){
uint16_t a = 0b0000000000000001;
cout<<"Little Endian = "<<a<<endl;
cout<<"Big Endian = "<<ntohs(a)<<endl;
uint16_t c = ntohs(a);
cout<<"Big Endian = "<<c<<endl;
cout<<"Little Endian = "<<htons(c)<<endl;
return 0;
}
OUTPUT :
Little Endian = 1
Big Endian = 256
Big Endian = 256
Little Endian = 1
because i'm using little endian system ntohs will convert 16 bit to the big endian and htons will convert big endian to little endian.
in the code example a variable contain 0000000000000001
but there is one thing that is confusing :
00000000 00000001 = big endian format
00000001 000000000 = little endian format
OUTPUT :
Little Endian = 1
Big Endian = 256
Big Endian = 256
Little Endian = 1
now the question is how this 00000001 000000000 is represent 1 because 00000001 000000000 little endian format and how this 00000001 000000000 is represent 256, because those byte doesn't look like 1 and 256

Actually,
ntohsandhtonsare both the same function. On a little endian system, these functions flip the byte order of a 16-bit integer. On a big endian system, both functions are no-ops and just return value passed in.This statement:
Is not printing little endian. It's just printing the value of
aindependent of what the actual architecture encoding for that value is. Or in layman's terms, it's "printing the value of a".If you want to see how the number is laid out sequentially in memory, you can do some simple casting hacks like this:
Technically, the above hack of casting a uint16_t* to a uint8_t* may trigger language lawyers to say "this is undefined behavior", but let's just go with it since it works well enough on most compilers and systems.
Let's extend your program to show what we can do with inspecting how values are laid out in memory.
And when run, it will print this:
Now let's change
ato somethign more fun like1234When the program is run with this change: