endianness

The reason behind endianness?

时光怂恿深爱的人放手 提交于 2019-11-29 20:50:47
So, I was wondering, why some architectures use little-endian and others big-endian. I remember I read somewhere that it has to do with performance, however, I don't understand how can endianness influence it. Also I know that: The little-endian system has the property that the same value can be read from memory at different lengths without using different addresses. Which seems a nice feature, but, even so, many systems use big-endian, which probably means big-endian has some advantages too (If so, which?). I'm sure there's more to it, most probably digging down to hardware level. Would love

Preferred idiom for endianess-agnostic reads

[亡魂溺海] 提交于 2019-11-29 18:37:34
问题 In the Plan 9 source code I often find code like this to read serialised data from a buffer with a well-defined endianess: #include <stdint.h> uint32_t le32read(uint8_t buf[static 4]) { return (buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24); } I expected both gcc and clang to compile this code into something as simple as this assembly on amd64: .global le32read .type le32read,@function le32read: mov (%rdi),%eax ret .size le32read,.-le32read But contrary to my expectations, neither gcc

Can someone explain this “endian-ness” function for me?

时光怂恿深爱的人放手 提交于 2019-11-29 18:03:23
问题 Write a program to determine whether a computer is big-endian or little-endian. bool endianness() { int i = 1; char *ptr; ptr = (char*) &i; return (*ptr); } So I have the above function. I don't really get it. ptr = (char*) &i, which I think means a pointer to a character at address of where i is sitting, so if an int is 4 bytes, say ABCD, are we talking about A or D when you call char* on that? and why? Would some one please explain this in more detail? Thanks. So specifically, ptr = (char*)

Finding if the system is little endian or big endian with perl

不想你离开。 提交于 2019-11-29 18:03:15
问题 Is there an option to find if my system is little endian byte order or big endian byte order using Perl? 回答1: perl -MConfig -e 'print "$Config{byteorder}\n";' See Perl documentation. 回答2: I guess you could do: $big_endian = pack("L", 1) eq pack("N", 1); This might fail if your system has a nonstandard (neither big-endian nor little-endian) byte ordering (eg PDP-11). 来源: https://stackoverflow.com/questions/2610849/finding-if-the-system-is-little-endian-or-big-endian-with-perl

How to convert 32 bit integer to network byte order?

被刻印的时光 ゝ 提交于 2019-11-29 17:28:02
How to convert the 32 bit integer to network byte order. What is the right way to do that? [1024].pack("N") OR [1,0,2,4].pack("N") Thanks To start, look at the output of each: >> [1024].pack("N") => "\000\000\004\000" >> [1,0,2,4].pack("N") => "\000\000\000\001" Note what the second is missing: >> [1,0,2,4].pack("NNNN") => "\000\000\000\001\000\000\000\000\000\000\000\002\000\000\000\004" 来源: https://stackoverflow.com/questions/12571715/how-to-convert-32-bit-integer-to-network-byte-order

C# Big-endian ulong from 4 bytes

南楼画角 提交于 2019-11-29 17:21:56
问题 Im trying to cast a 4 byte array to an ulong in C#. I'm currently using this code: atomSize = BitConverter.ToUInt32(buffer, 0); The byte[4] contains this: 0 0 0 32 However, the bytes are Big-Endian. Is there a simple way to convert this Big-Endian ulong to a Little-Endian ulong? 回答1: I believe that the EndianBitConverter in Jon Skeet's MiscUtil library (nuget link) can do what you want. You could also swap the bits using bit shift operations: uint swapEndianness(uint x) { return ((x &

Network byte order and endianness issues

南楼画角 提交于 2019-11-29 16:03:21
I read on internet that standard byte order for networks is big endian, also known as network byte order. Before transferring data on network, data is first converted to network byte order (big endian). But can any one please let me know who will take care of this conversion. Whether the code developer do really worry about this endianness? If yes, can you please let me know the examples where we need to take care (in case of C, C++). In C and C++, you will have to worry about endianness in low level network code. Typically the serialization and deserialization code will call a function or

when to use hton/ntoh and when to convert data myself?

假装没事ソ 提交于 2019-11-29 15:28:27
to convert a byte array from another machine which is big-endian, we can use: long long convert(unsigned char data[]) { long long res; res = 0; for( int i=0;i < DATA_SIZE; ++i) res = (res << 8) + data[i]; return res; } if another machine is little-endian, we can use long long convert(unsigned char data[]) { long long res; res = 0; for( int i=DATA_SIZE-1;i >=0 ; --i) res = (res << 8) + data[i]; return res; } why do we need the above functions? shouldn't we use hton at sender and ntoh when receiving? Is it because hton/nton is to convert integer while this convert() is for char array? The hton /

Signed right shift = strange result?

走远了吗. 提交于 2019-11-29 14:38:43
I was helping someone with their homework and ran into this strange issue. The problem is to write a function that reverses the order of bytes of a signed integer(That's how the function was specified anyway), and this is the solution I came up with: int reverse(int x) { int reversed = 0; reversed = (x & (0xFF << 24)) >> 24; reversed |= (x & (0xFF << 16)) >> 8; reversed |= (x & (0xFF << 8)) << 8; reversed |= (x & 0xFF) << 24; return reversed; } If you pass 0xFF000000 to this function, the first assignment will result in 0xFFFFFFFF . I don't really understand what is going on, but I know it has

Confusion in htons- little endian/ big endian

╄→尐↘猪︶ㄣ 提交于 2019-11-29 13:42:43
问题 When I send a integer variable from one process to other through socket, and then printing the value at received end, the value is still the same without using ntohl/htonl, then where do I need to use these functions other than initializing socket structures. I understand litte/big endian. But why do we need to convert port and IP nos to host/network byte order when value remains the same. Please explain in detail how the integer is tranferred over network? 回答1: If you want your program to be