endianness

endianess detection and performance in C

风流意气都作罢 提交于 2019-12-23 05:41:44
问题 I've got a performance critical C code that needs to work on a variety of platform. Some of them are little endian, others are big endian. Detecting endianess is currently an imperfect process, based on macro detection. But it's difficult to be sure that the macro detection will work for all combinations of systems and compilers. Welcome to the world of portable code. One relatively safe way to detect endianess is to use a runtime test, and hope that it will get optimized out by the compiler.

How to get the double value using libpq?

故事扮演 提交于 2019-12-22 10:00:04
问题 The examples in the libpq documentation show how to get the the integer value by converting it to the host-endian representation. I am curious what must be done to get the double precision value using libpq (without libpqtyppes)? I have tried reinterpret_cast with no luck. Also why text and byte data doesn't need endian conversions? The DB runs locally on Windows 7, I am using Visual C++ 2013. pptr is a double vaule I am trying to retrieve. #include <iostream> #include <memory> #include

Swap bits in c++ for a double

三世轮回 提交于 2019-12-22 08:59:29
问题 Im trying to change from big endian to little endian on a double. One way to go is to use double val, tmp = 5.55; ((unsigned int *)&val)[0] = ntohl(((unsigned int *)&tmp)[1]); ((unsigned int *)&val)[1] = ntohl(((unsigned int *)&tmp)[0]); But then I get a warning: "dereferencing type-punned pointer will break strict-aliasing rules" and I dont want to turn this warning off. Another way to go is: #define ntohll(x) ( ( (uint64_t)(ntohl( (uint32_t)((x << 32) >> 32) )) << 32) | ntohl( ((uint32_t)(x

Relation between endianness and stack-growth direction

狂风中的少年 提交于 2019-12-22 08:00:05
问题 Is there a relation between endianness of a processor and the direction of stack growth? For example, x86 architecture is little endian and the stack grows downwards (i.e. it starts at highest address and grows towards lower address with each push operation). Similarly, in SPARC architecture , which is big endian , the stack starts at lowest address and grows upwards towards higher addresses. This relationship pattern is seen in almost all architectures. I believe there must be a reason for

Send a struct over a socket with correct padding and endianness in C

心不动则不痛 提交于 2019-12-22 06:49:51
问题 I have several structures defined to send over different Operating Systems (tcp networks). Defined structures are: struct Struct1 { uint32_t num; char str[10]; char str2[10];} struct Struct2 { uint16_t num; char str[10];} typedef Struct1 a; typedef Struct2 b; The data is stored in a text file. Data Format is as such: 123 Pie Crust Struct1 a is stored as 3 separate parameters. However, struct2 is two separate parameters with both 2nd and 3rd line stored to the char str[] . The problem is when

Finding endian-ness programmatically at compile-time using C++11

安稳与你 提交于 2019-12-22 03:35:15
问题 I have referred many questions in SO on this topic, but couldn't find any solution so far. One natural solution was mentioned here: Determining endianness at compile time. However, the related problems mentioned in the comments & the same answer. With some modifications, I am able to compile a similar solution with g++ & clang++ ( -std=c++11 ) without any warning. static_assert(sizeof(char) == 1, "sizeof(char) != 1"); union U1 { int i; char c[sizeof(int)]; }; union U2 { char c[sizeof(int)];

What should I #include to use 'htonl'?

拈花ヽ惹草 提交于 2019-12-22 01:57:23
问题 I want to use the htonl function in my ruby c extension, but don't want to use any of the other internet stuff that comes with it. What would be the most minimalistic file to #include that is still portable? Looking through the header files on my computer, I can see that either machine/endian.h or sys/_endian.h would let me use them, although I am not sure if that is a good idea. 回答1: The standard header is: #include <arpa/inet.h> You don't have to worry about the other stuff defined in that

Java Converting long to bytes - which approach is more efficient

为君一笑 提交于 2019-12-21 20:39:50
问题 I have two approaches to convert long to byte array. for (int i = 0; i < 7; i++) { data[pos + i] = (byte) (value >> (7- i - 1 << 3)); } and for (int i = 7; i >= 0; --i) { data[p + i] = (byte)(newl & 0xff); newl >>= 8; } which of the two operations is more efficient? 回答1: I suggest you look at how the Java code does it. public final void writeLong(long v) throws IOException { writeBuffer[0] = (byte)(v >>> 56); writeBuffer[1] = (byte)(v >>> 48); writeBuffer[2] = (byte)(v >>> 40); writeBuffer[3]

JavaScript equivalent to htonl?

感情迁移 提交于 2019-12-21 19:53:29
问题 For an AJAX request, I need to send a magic number as the first four bytes of the request body, most significant byte first, along with several other (non-constant) values in the request body. Is there something equivalent to htonl in JavaScript? For example, given 0x42656566, I need to produce the string "Beef". Unfortunately, my number is along the lines of 0xc1ba5ba9. When the server reads the request, it is getting the value -1014906182 (instead of -1044751447). 回答1: There's no built-in

why endianess matters among bits inside a byte?

雨燕双飞 提交于 2019-12-21 17:39:04
问题 the following is the IP structure from library on a linux machine struct ip { #if __BYTE_ORDER == __LITTLE_ENDIAN unsigned int ip_hl:4; /* header length */ unsigned int ip_v:4; /* version */ #endif #if __BYTE_ORDER == __BIG_ENDIAN unsigned int ip_v:4; /* version */ unsigned int ip_hl:4; /* header length */ #endif u_int8_t ip_tos; /* type of service */ u_short ip_len; /* total length */ u_short ip_id; /* identification */ u_short ip_off; /* fragment offset field */ #define IP_RF 0x8000 /*