endianness

Cross-platform definition of _byteswap_uint64 and _byteswap_ulong

﹥>﹥吖頭↗ 提交于 2019-12-12 15:08:41
问题 Visual Studio defines _byteswap_uint64 and _byteswap_ulong in stdlib.h. Am I right to assume, that this is not standard and won't compile on Linux or Darwin? Is there a way to define these includes in a cross-platform way? 回答1: Google's CityHash source code uses this code: https://github.com/google/cityhash/blob/8af9b8c2b889d80c22d6bc26ba0df1afb79a30db/src/city.cc#L50 #ifdef _MSC_VER #include <stdlib.h> #define bswap_32(x) _byteswap_ulong(x) #define bswap_64(x) _byteswap_uint64(x) #elif

Read odd addresses, half words?

北慕城南 提交于 2019-12-12 13:36:15
问题 It's common knowledge that many CPU architectures (ARM, PPC) can not read odd addresses but will generate an exception if forced too, and yet others can, but do so slightly slower. (x86) But is there any CPU which can only address full 32 bit (or even larger!) words? I.e. it can not address 16 bit words? Perhaps amd64? I am trying to write a portable yet fast C malloc like allocator and want to align my memory accesses properly. Currently I am targeting ARM, i386 and amd64, and these I could

Convert between little-endian and big-endian floats effectively

孤街浪徒 提交于 2019-12-12 13:13:08
问题 I have a working software, which currently runs on a little-endian architecture. I would like to make it run in big-endian mode too. I would like to write little-endian data into files, regardless of the endianness of the underlying system. To achieve this, I decided to use the boost endian library. It can convert integers efficiently. But it cannot handle floats (and doubles). It states in the documentation, that " Floating point types will be supported in the Boost 1.59.0 ". But they are

Endianness on DataInputStream

旧时模样 提交于 2019-12-12 11:14:26
问题 I have a file format that I want to load that contains raw data for OpenGL objects. All would be well but that data is encoded little endian. Is there a java class that does the exact job of DataInputStream but using little endian or do I have to load it byte by byte and perform the conversions myself? 回答1: DataInputStream is only big endian. If you use ByteBuffer you can change the endianness with buffer.order(ByteOrder.LITTLE_ENDIAN); 来源: https://stackoverflow.com/questions/13211770

How to test endianness in node.js

妖精的绣舞 提交于 2019-12-12 10:45:38
问题 When you read a chunk of bytes and you need to convert them to a number, node.js has functions like buffer.readInt32BE() and buffer.readInt32LE() . If I only know that the first 4 bytes of a file is an integer, what function should I use if I don't know the endianness of the system? Big endian or little endian? Doing a fast googling (stackoverflow), in C we can test the endianness doing: if ( htonl(47) == 47 ) { // Big endian } else { // Little endian. } How can we test the endianness in node

boost asio and endian

两盒软妹~` 提交于 2019-12-12 08:36:46
问题 I cant tell, does boost asio handle endian? 回答1: Asio does convert things like port into network order. The conversion functions are not exposed as part of the official interface and are hidden in the detail namespace instead (e.g. boost::asio::detail::socket_ops::host_to_network_short ). 回答2: boost::asio::socket_'s do not perform any byte order conversion. 来源: https://stackoverflow.com/questions/524453/boost-asio-and-endian

how to convert big-endian numbers to native numbers delphi

牧云@^-^@ 提交于 2019-12-12 08:07:45
问题 I want to know how to convert big-endian numbers to native numbers in Delphi. I am porting some C++ code in that I came across: unsigned long blockLength = *blockLengthPtr++ << 24; blockLength |= *blockLengthPtr++ << 16; blockLength |= *blockLengthPtr++ << 8; blockLength |= *blockLengthPtr; unsigned long dataLength = *dataLengthPtr++ << 24; dataLength |= *dataLengthPtr++ << 16; dataLength |= *dataLengthPtr++ << 8; dataLength |= *dataLengthPtr; I am not familiar with C++, so I don't understand

Portable C binary serialization primitives

一个人想着一个人 提交于 2019-12-12 07:49:37
问题 As far as I know, the C library provides no help in serializing numeric values into a non-text byte stream. Correct me if I'm wrong. The most standard tool in use is htonl et al from POSIX. These functions have shortcomings: There is no 64-bit support. There is no floating-point support. There are no versions for signed types. When deserializing, the unsigned-to-signed conversion relies on signed integral overflow which is UB. Their names do not state the size of the datatype. They depend on

Endianness manipulation - is there a C library for this?

假如想象 提交于 2019-12-12 07:49:31
问题 With the sort of programs I write (working with raw file data) I often need functions to convert between big and little endian. Usually I write these myself (which is covered by many other posts here) but I'm not that keen on doing this for a number of reasons - the main one being lack of testing. I don't really want to spend ages testing my code in a big endian emulator, and often just omit the code for big endian machines altogether. I also would rather make use of faster functions provided

How can I convert little endian to big endian using htonl

江枫思渺然 提交于 2019-12-12 05:27:22
问题 I have struct with the following elements. Plus the structure is complety padded. typedef struct { uint16_t a; uint16_t b; uint8_t c; uint8_t d; uint8_t e[6]; } ad; This structure is a little endian. I mean when I print this structure on my big endian machine I get the following if c=1 , d=2, e[0] =3, e[1]=4. I get c=4, d=3, e[0] = 2 and e[1]=1. a and b are swapped. further, e[1] is swapped with c and e[0] is swapped with d. I am using htonl function like the following. but, it is not working