endianness

Quickest way to change endianness

我怕爱的太早我们不能终老 提交于 2019-12-03 14:02:31
问题 What is the quickest way to reverse the endianness of a 16 bit and 32 bit integer. I usually do something like (this coding was done in Visual Studio in C++): union bytes4 { __int32 value; char ch[4]; }; union bytes2 { __int16 value; char ch[2]; }; __int16 changeEndianness16(__int16 val) { bytes2 temp; temp.value=val; char x= temp.ch[0]; temp.ch[0]=temp.ch[1]; temp.ch[1]=x; return temp.value; } __int32 changeEndianness32(__int32 val) { bytes4 temp; temp.value=val; char x; x= temp.ch[0]; temp

Portable C binary serialization primitives

本小妞迷上赌 提交于 2019-12-03 12:11:13
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 8-bit bytes and the presence of exact-size uint_ N _t. The input types are the same as the output types

Big endian or Little endian on net?

有些话、适合烂在心里 提交于 2019-12-03 11:15:25
问题 In what byte order does data transfer occur on net? Is it Little Endian or big endian? How is it converted to the respective byte order once the data reaches the host ? 回答1: "Network byte order" is Big Endian, and protocols such as TCP use this for integer fields (e.g. port numbers). Functions such as htons and ntohs can be used to do conversion. The data itself doesn't have any endianness it's entirely application defined, unless you're using a Presentation Layer such as XDR. 回答2: Its

JavaScript Endian Encoding?

你。 提交于 2019-12-03 10:19:55
问题 A response on SO got me thinking, does JavaScript guarantee a certain endian encoding across OSs and browsers? Or put another way are bitwise shifts on integers "safe" in JavaScript? 回答1: Yes, they are safe. Although you're not getting the speed benefits you might hope for since JS bit operations are "a hack". 回答2: Shifting is safe, but your question is flawed because endianness doesn't affect bit-shift operations anyway. Shifting left is the same on big-endian and little-endian systems in

Converting Endianess on a bit field structure

心已入冬 提交于 2019-12-03 07:49:35
问题 I need to convert a bit-field structure from little-endian to big-endia architecture. What is the best way to do that, as there will be issues in byte boundaries, if I simply swap the structure elements. Ex Structure is: struct { unsigned int b1:1; unsigned int b2:8; unsigned int b3:7; unsigned int b4:8; unsigned int b5:7; unsigned int b6:1; }; 回答1: You could use a 32 bit integer, and extract information out of it using and- and bitshift operators. With that in place, you could simply use

Why does an 8-bit field have endianness?

痞子三分冷 提交于 2019-12-03 06:08:20
See the definition of TCP header in /netinet/tcp.h: struct tcphdr { u_int16_t th_sport; /* source port */ u_int16_t th_dport; /* destination port */ tcp_seq th_seq; /* sequence number */ tcp_seq th_ack; /* acknowledgement number */ # if __BYTE_ORDER == __LITTLE_ENDIAN u_int8_t th_x2:4; /* (unused) */ u_int8_t th_off:4; /* data offset */ # endif # if __BYTE_ORDER == __BIG_ENDIAN u_int8_t th_off:4; /* data offset */ u_int8_t th_x2:4; /* (unused) */ # endif u_int8_t th_flags; # define TH_FIN 0x01 # define TH_SYN 0x02 # define TH_RST 0x04 # define TH_PUSH 0x08 # define TH_ACK 0x10 # define TH_URG

(java) Writing in file little endian

我们两清 提交于 2019-12-03 05:59:58
I'm trying to write TIFF IFDs, and I'm looking for a simple way to do the following (this code obviously is wrong but it gets the idea across of what I want): out.writeChar(12) (bytes 0-1) out.writeChar(259) (bytes 2-3) out.writeChar(3) (bytes 4-5) out.writeInt(1) (bytes 6-9) out.writeInt(1) (bytes 10-13) Would write: 0c00 0301 0300 0100 0000 0100 0000 I know how to get the writing method to take up the correct number of bytes (writeInt, writeChar, etc) but I don't know how to get it to write in little endian. Anyone know? Maybe you should try something like this: ByteBuffer buffer =

git svn rebase resulted in “byte order is not compatible” error

寵の児 提交于 2019-12-03 05:46:20
问题 Following is the error I am getting when I tried 'git svn rebase': Byte order is not compatible at ../../lib/Storable.pm (autosplit into ../../lib/auto/Storable/_retrieve.al) line 380, at /usr/lib/perl5/5.10/Memoize/Storable.pm line 21 The version of perl I am running is: $ perl --version This is perl, v5.10.1 (*) built for i686-cygwin-thread-multi-64int (with 12 registered patches, see perl -V for more detail) When I searched the web for " Byte order is not compatible " and I get numerous

Little endian Vs Big endian

夙愿已清 提交于 2019-12-03 05:19:24
问题 Lets say I have 4Byte integer and I want to cast it to 2Byte short integer. Am I right that in both (little and big endian) short integer will consist of 2 least significant bytes of this 4Byte integer? Second question: What will be the result of such code in little endian and big endian processor? int i = some_number; short s = *(short*)&i; IMHO in big endian processor 2 most significant bytes would be copied, and in little endian 2 least significant bytes would be copied. 回答1: Am I right

Reverse the Endianness of a C structure

六月ゝ 毕业季﹏ 提交于 2019-12-03 04:05:53
I have a structure in C that looks like this: typedef u_int8_t NN; typedef u_int8_t X; typedef int16_t S; typedef u_int16_t U; typedef char C; typedef struct{ X test; NN test2[2]; C test3[4]; U test4; } Test; I have declared the structure and written values to the fields as follows: Test t; int t_buflen = sizeof(t); memset( &t, 0, t_buflen); t.test = 0xde; t.test2[0]=0xad; t.test2[1]=0x00; t.test3[0]=0xbe; t.test3[1]=0xef; t.test3[2]=0x00; t.test3[3]=0xde; t.test4=0xdeca; I am sending this structure via UDP to a server. At present this works fine when I test locally, however I now need to send