endianness

How to write constexpr swap function to change endianess of an integer?

送分小仙女□ 提交于 2019-12-03 03:47:12
问题 How to write a constexpr function to swap endianess of an integer, without relying on compiler extensions and can you give an example on how to do it? 回答1: Yes, it's pretty easy; here's a recursive (C++11-compatible) implementation (unsigned integral types only): #include <climits> #include <cstdint> #include <type_traits> template<class T> constexpr typename std::enable_if<std::is_unsigned<T>::value, T>::type bswap(T i, T j = 0u, std::size_t n = 0u) { return n == sizeof(T) ? j : bswap<T>(i >

Undefined behavior from pointer math on a C++ array

会有一股神秘感。 提交于 2019-12-03 01:26:13
Why the output of this program is 4 ? #include <iostream> int main() { short A[] = {1, 2, 3, 4, 5, 6}; std::cout << *(short*)((char*)A + 7) << std::endl; return 0; } From my understanding, on x86 little endian system, where char has 1 byte, and short 2 bytes, the output should be 0x0500 , because the data in array A is as fallow in hex: 01 00 02 00 03 00 04 00 05 00 06 00 We move from the beginning 7 bytes forward, and then read 2 bytes. What I'm missing? You are violating strict aliasing rules here. You can't just read half-way into an object and pretend it's an object all on its own. You can

What's the most Pythonic way of determining endianness?

放肆的年华 提交于 2019-12-03 00:59:17
I'm trying to find the best way of working out whether the machine my code is running on is big-endian or little-endian. I have a solution that works (although I haven't tested it on a big-endian machine) but it seems a bit clunky: import struct little_endian = (struct.pack('@h', 1) == struct.pack('<h', 1)) This is just comparing a 'native' two-byte pack to a little-endian pack. Is there a prettier way? The answer is in the sys module : >>> import sys >>> sys.byteorder 'little' Of course depending on your machine it may return 'big' . Your method should certainly work too though. 来源: https:/

Memory contents to ASCII string, little endian format

风流意气都作罢 提交于 2019-12-02 19:09:43
问题 I have to translate the following memory contents to ASCII code, using little endian format: 0x6A636162 0x64726177 0x00002173 I got "jcab draw! s", which is wrong (and of course, is complete nonsense). How are you supposed to do this using little endian format? 回答1: It's supposed to be "backwards!" You read the first two rightmost hex values first as one ascii char, and then focus on the pair to the left of that, until you get to the furthest left most hex values. 来源: https://stackoverflow

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

大城市里の小女人 提交于 2019-12-02 19:04:46
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 hits that shows the Perl doc that says: What this means is that if you have data written by Storable 1.x

Little endian Vs Big endian

て烟熏妆下的殇ゞ 提交于 2019-12-02 18:35:29
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. Am I right that in both short integer will consist of 2 least significant bytes of this 4Byte integer? Yes, by definition

Big endian and Little endian representations

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 13:15:34
If I write the following section .data align 4 X: db 1 Y: dw 5 Z: db 0x11 section .text add dword [X], 0xAA000101 I'm trying to understand the differences between the big endian and the little endian representations, and I don't understand what will be the value of each variable for each representation? Will they be the same? Have a look at these pictures: This is the list of endiannesses for all architectures/instruction sets In a big-endian configuration, the most significant byte of a doubleword (32-bits on x86) is stored in the smallest address, and the least significant byte is stored in

struct sockaddr_in member byte order for bind()

纵饮孤独 提交于 2019-12-02 10:46:14
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 subsequent example further down at the same noted site has the following code segment: 030 30: struct

Convert Little-endian ByteArray to Big-endian in AS3

大憨熊 提交于 2019-12-02 10:05:36
How to convert Little-endian ByteArray to Big-endian in AS3? I convert bitmapData to Big-endian ByteArray and then push it into memory with Adobe Alchemy. And then when i read it from memory i get Little-endian ByteArray. How to get Big-endian. I use this example code http://blog.debit.nl/2009/03/using-bytearrays-in-actionscript-and-alchemy/ (Memory allocation in C with direct access in Actionscript (FAST!!)) Code: var ba:ByteArray = currentBitmapData.getPixels( currentBitmapData.rect ); ba.position = 0; var ns:Namespace = new Namespace("cmodule.al_exam"); var data:ByteArray = (ns::gstate).ds;

Memory contents to ASCII string, little endian format

回眸只為那壹抹淺笑 提交于 2019-12-02 09:23:15
I have to translate the following memory contents to ASCII code, using little endian format: 0x6A636162 0x64726177 0x00002173 I got "jcab draw! s", which is wrong (and of course, is complete nonsense). How are you supposed to do this using little endian format? It's supposed to be "backwards!" You read the first two rightmost hex values first as one ascii char, and then focus on the pair to the left of that, until you get to the furthest left most hex values. 来源: https://stackoverflow.com/questions/10369625/memory-contents-to-ascii-string-little-endian-format