endianness

How to test your code on a machine with big-endian architecture?

Deadly 提交于 2019-11-29 05:39:28
Both ideone.com and codepad.org have Little-Endian architechtures. I want to test my code on some machine with Big-Endian architechture (for example - Solaris - which I don't have). Is there some easy way that you know about? Googling "big endian online emulator" lead me to PearPC . I assume that if you have the patience you can install Mandrake Linux , get gcc, and go party. QEMU supports emulating several big-endian architectures. Note that some architectures support both endiannesses; some (Itanium, ARM) are primarily used as little-endian while others (PowerPC, MIPS) are primarily used as

dealing with endianness in c++

自闭症网瘾萝莉.ら 提交于 2019-11-29 05:15:37
I am working on translating a system from python to c++. I need to be able to perform actions in c++ that are generally performed by using Python's struct.unpack (interpreting binary strings as numerical values). For integer values, I am able to get this to (sort of) work, using the data types in stdint.h : struct.unpack("i", str) ==> *(int32_t*) str; //str is a char* containing the data This works properly for little-endian binary strings, but fails on big-endian binary strings. Basically, I need an equivalent to using the > tag in struct.unpack: struct.unpack(">i", str) ==> ??? Please note,

Converting 32-bit unsigned integer (big endian) to long and back

大憨熊 提交于 2019-11-29 02:13:26
I have a byte[4] which contains a 32-bit unsigned integer (in big endian order) and I need to convert it to long (as int can't hold an unsigned number). Also, how do I do it vice-versa (i.e. from long that contains a 32-bit unsigned integer to byte[4])? Sounds like a work for the ByteBuffer . Somewhat like public static void main(String[] args) { byte[] payload = toArray(-1991249); int number = fromArray(payload); System.out.println(number); } public static int fromArray(byte[] payload){ ByteBuffer buffer = ByteBuffer.wrap(payload); buffer.order(ByteOrder.BIG_ENDIAN); return buffer.getInt(); }

How do you write (portably) reverse network byte order?

自作多情 提交于 2019-11-29 02:01:02
Background When designing binary file formats, it's generally recommended to write integers in network byte order. For that, there are macros like htonhl() . But for a format such as WAV, actually the little endian format is used. Question How do you portably write little endian values, regardless of if the CPU your code runs on is a big endian or little endian architecture? (Ideas: can the standard macros ntohl() and htonl() be used "in reverse" somehow? Or should the code just test runtime if it's running on a little or big endian CPU and choose the appropriate code path?) So the question is

Safely punning char* to double in C

拥有回忆 提交于 2019-11-29 01:31:47
问题 In an Open Source program I wrote, I'm reading binary data (written by another program) from a file and outputting ints, doubles, and other assorted data types. One of the challenges is that it needs to run on 32-bit and 64-bit machines of both endiannesses, which means that I end up having to do quite a bit of low-level bit-twiddling. I know a (very) little bit about type punning and strict aliasing and want to make sure I'm doing things the right way. Basically, it's easy to convert from a

Fast reading of little endian integers from file

匆匆过客 提交于 2019-11-28 23:45:57
I need to read a binary file consisting of 4 byte integers (little endian) into a 2D array for my Android application. My current solution is the following: DataInputStream inp = null; try { inp = new DataInputStream(new BufferedInputStream(new FileInputStream(procData), 32768)); } catch (FileNotFoundException e) { Log.e(TAG, "File not found"); } int[][] test_data = new int[SIZE_X][SIZE_Y]; byte[] buffer = new byte[4]; ByteBuffer byteBuffer = ByteBuffer.allocate(4); for (int i=0; i < SIZE_Y; i++) { for (int j=0; j < SIZE_X; j++) { inp.read(buffer); byteBuffer = ByteBuffer.wrap(buffer); test

Convert “little endian” hex string to IP address in Python

喜你入骨 提交于 2019-11-28 19:53:31
What's the best way to turn a string in this form into an IP address: "0200A8C0" . The "octets" present in the string are in reverse order, i.e. the given example string should generate 192.168.0.2 . Network address manipulation is provided by the socket module. socket.inet_ntoa(packed_ip) Convert a 32-bit packed IPv4 address (a string four characters in length) to its standard dotted-quad string representation (for example, ‘123.45.67.89’). This is useful when conversing with a program that uses the standard C library and needs objects of type struct in_addr, which is the C type for the 32

constexpr and endianness

♀尐吖头ヾ 提交于 2019-11-28 19:15:00
A common question that comes up from time to time in the world of C++ programming is compile-time determination of endianness. Usually this is done with barely portable #ifdefs. But does the C++11 constexpr keyword along with template specialization offer us a better solution to this? Would it be legal C++11 to do something like: constexpr bool little_endian() { const static unsigned num = 0xAABBCCDD; return reinterpret_cast<const unsigned char*> (&num)[0] == 0xDD; } And then specialize a template for both endian types: template <bool LittleEndian> struct Foo { // .... specialization for

How can I find Endian-ness of my PC programmatically using C? [duplicate]

浪子不回头ぞ 提交于 2019-11-28 18:53:31
Possible Duplicate: Detecting endianness programmatically in a C++ program Is there any library function available to find the endian-ness of my PC? COD3BOY Why you need a library if you can find it like this? :) int num = 1; if (*(char *)&num == 1) { printf("Little-Endian\n"); } else { printf("Big-Endian\n"); } I'm not aware of a library function. You can get the address of an integer, then treat that address as a character pointer and write data into the bytes that comprise the integer. Then, read out what is actually in the integer and see if you get a result consistent with a big endian or

How do I swap endian-ness (byte order) of a variable in javascript

假装没事ソ 提交于 2019-11-28 18:24:07
I am receiving and sending a decimal representation of two little endian numbers. I would like to: shift one variable 8 bits left OR them shift a variable number of bits create 2 8 bit numbers representing the first and second half of the 16 bit number. javascript (according to https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators ) uses big endian representation when shifting... endianness is a bit foreign to me (I am only 90 percent sure that my outlined steps are what i want.) so swapping is a bit dizzying. please help! I only really need to know how to swap the