endianness

Faster way to swap endianness in C# with 16 bit words

爱⌒轻易说出口 提交于 2019-12-04 08:48:21
问题 There's got to be a faster and better way to swap bytes of 16bit words then this.: public static void Swap(byte[] data) { for (int i = 0; i < data.Length; i += 2) { byte b = data[i]; data[i] = data[i + 1]; data[i + 1] = b; } } Does anyone have an idea? 回答1: In my attempt to apply for the Uberhacker award, I submit the following. For my testing, I used a Source array of 8,192 bytes and called SwapX2 100,000 times: public static unsafe void SwapX2(Byte[] source) { fixed (Byte* pSource = &source

Netty and ByteOrder

霸气de小男生 提交于 2019-12-04 07:22:33
Due to poor documentation and lack of experience with Netty, i faced with little problem. I have no clue how can i set a default ByteOrder. I need a Little-Endian set by default. I'll be glad, if someone will give me some hints about this. You could use Bootstrap.setOption() to do this. serverBootstrap.setOption("child.bufferFactory", new HeapChannelBufferFactory(ByteOrder.LITTLE_ENDIAN)); ... or ... clientBootstrap.setOption("bufferFactory", new HeapChannelBufferFactory(ByteOrder.LITTLE_ENDIAN)); 来源: https://stackoverflow.com/questions/9758537/netty-and-byteorder

Is vec_sld endian sensitive?

北慕城南 提交于 2019-12-04 07:09:32
I'm working on a PowerPC machine with in-core crypto. I'm having trouble porting AES key expansion from big endian to little endian using built-ins. Big endian works, but little endian does not. The algorithm below is the snippet presented in an IBM blog article . I think I have the issue isolated to line 2 below: typedef __vector unsigned char uint8x16_p8; uint8x64_p8 r0 = {0}; r3 = vec_perm(r1, r1, r5); /* line 1 */ r6 = vec_sld(r0, r1, 12); /* line 2 */ r3 = vcipherlast(r3, r4); /* line 3 */ r1 = vec_xor(r1, r6); /* line 4 */ r6 = vec_sld(r0, r6, 12); /* line 5 */ r1 = vec_xor(r1, r6); /*

How to byteswap a double?

。_饼干妹妹 提交于 2019-12-04 06:27:46
I'm trying to write a byteswap routine for a C++ program running on Win XP. I'm compiling with Visual Studio 2008. This is what I've come up with: int byteswap(int v) // This is good { return _byteswap_ulong(v); } double byteswap(double v) // This doesn't work for some values { union { // This trick is first used in Quake2 source I believe :D __int64 i; double d; } conv; conv.d = v; conv.i = _byteswap_uint64(conv.i); return conv.d; } And a function to test: void testit() { double a, b, c; CString str; for (a = -100; a < 100; a += 0.01) { b = byteswap(a); c = byteswap(b); if (a != c) { str

struct sockaddr_in member byte order for bind()

心已入冬 提交于 2019-12-04 06:15:59
问题 I'm learning socket programming and am confused by what I feel is inconsistent use of htons() and family of functions in my learning material. I'm currently reading this site which has the following code segment: 001 1: struct sockaddr_in adr_inet; 002 2: int adr_len; 003 3: 004 4: memset(&adr_inet,0,sizeof adr_inet); 005 5: 006 6: adr_inet.sin_family = AF_INET; 007 7: adr_inet.sin_port = ntohs(0); 008 8: adr_inet.sin_addr.s_addr = ntohl(INADDR_ANY); 009 9: adr_len = sizeof adr_inet; A

Node.JS Big-Endian UCS-2

只谈情不闲聊 提交于 2019-12-04 05:59:21
I'm working with Node.JS. Node's buffers support little-endian UCS-2, but not big-endian, which I need. How would I do so? According to wikipedia, UCS-2 should always be big-endian so it's odd that node only supports little endian. You might consider filing a bug. That said, switching endian-ness is fairly straight-forward since it's just a matter of byte order. So just swap bytes around to go back and forth between little and big endian, like so: function swapBytes(buffer) { var l = buffer.length; if (l & 0x01) { throw new Error('Buffer length must be even'); } for (var i = 0; i < l; i += 2)

Little-endian and Big-endian

被刻印的时光 ゝ 提交于 2019-12-04 05:47:01
问题 I must write a routine for conversion between the 2 representations. But I'm a bit confused. If I have an architecture with a memory with words of 32 bits and I must store the word 0xA15D23B1 with Big-endian the memory become A1 after 5D after 23 and in the end B1 with Little-endian rather the memory is B1 after 23 after 5D and after A1 Is it right? If I can address the individual bytes on my machine and the address of my word is zero who is the byte 1? In Big-endian is 5D? and in little

How write big endian ByteBuffer to little endian in Java

房东的猫 提交于 2019-12-04 04:03:13
I currently have a Java ByteBuffer that already has the data in Big Endian format. I then want to write to a binary file as Little Endian. Here's the code which just writes the file still in Big Endian: public void writeBinFile(String fileName, boolean append) throws FileNotFoundException, IOException { FileOutputStream outStream = null; try { outStream = new FileOutputStream(fileName, append); FileChannel out = outStream.getChannel(); byteBuff.position(byteBuff.capacity()); byteBuff.flip(); byteBuff.order(ByteOrder.LITTLE_ENDIAN); out.write(byteBuff); } finally { if (outStream != null) {

boost asio and endian

好久不见. 提交于 2019-12-04 03:59:47
I cant tell, does boost asio handle endian? 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 ). boost::asio::socket_'s do not perform any byte order conversion. 来源: https://stackoverflow.com/questions/524453/boost-asio-and-endian

How to BSWAP the lower 32-bit of 64-bit register?

放肆的年华 提交于 2019-12-04 03:17:04
I've been looking for the answer for how to use BSWAP for lower 32-bit sub-register of 64-bit register. For example, 0x0123456789abcdef is inside RAX register, and I want to change it to 0x01234567efcdab89 with a single instruction (because of performance). So I tried following inline function: #define BSWAP(T) { \ __asm__ __volatile__ ( \ "bswap %k0" \ : "=q" (T) \ : "q" (T)); \ } And the result was 0x00000000efcdab89 . I don't understand why the compiler acts like this. Does anybody know the efficient solution? Ah, yes, I understand the problem now: the x86-64 processors implicitly zero