endianness

PNG file format endianness?

泄露秘密 提交于 2019-11-29 13:23:00
Im not sure if endian is the right word but.. I have been parsing through a PNG file and I have noticed that all of the integer values are in big endian. Is this true? For example, the width and height are stored in the PNG file as 32bit unsigned integers. My image is 16x16 and in the file its stored as: 00 00 00 10 when it should be: 10 00 00 00 Is this true or is there something I am missing? Yes, according to the specification, integers must be in network byte order (big endian): All integers that require more than one byte shall be in network byte order: the most significant byte comes

How are the union members stored?

拟墨画扇 提交于 2019-11-29 12:33:52
问题 union test { int i; char ch; }t; int main() { t.ch=20; } Suppose sizeof(int)==2 and let the memory addresses allocated for t are 2000, 2001. Then where is 20 i.e. t.ch stored - at 2000 or 2001 or depends on endianness of machine? 回答1: The C99 standard (§6.7.2.1.14) says: The size of a union is sufficient to contain the largest of its members. The value of at most one of the members can be stored in a union object at any time. A pointer to a union object, suitably converted, points to each of

Reverse byte order of EAX register

蹲街弑〆低调 提交于 2019-11-29 12:08:45
Example: 0xAABBCCDD will turn into 0xDDCCBBAA My program crashes, due to Access Violation exception right in the first XOR operation. It seems like there's a better naive solution, using shifting or rotating, but anyways, here's the code: ;; ######################################################################### .486 .model flat, stdcall option casemap :none ; case sensitive ;; ######################################################################### include \masm32\include\masm32.inc include \masm32\include\kernel32.inc includelib \masm32\lib\kernel32.lib includelib \masm32\lib\masm32.lib

Bit conversion tool in Objective-C

為{幸葍}努か 提交于 2019-11-29 11:39:28
Are there any built in utilities or macros in the objective-c libraries for iOS that will allow you to convert bytes to and from integers with respect to endianess? Please don't tell me to use bit-shifting operations. I am trying to avoid writing custom code to do this if it already exists. I would like the code to convert NSData* to primitive types (int, uint, short, etc) and to convert primitive types back to NSData*. You can get the bytes from NSData by accessing the bytes property. Then just cast that to a pointer to whatever type you want. Obviously you'll need to ensure you know the

How to byte-swap a 32-bit integer in python?

回眸只為那壹抹淺笑 提交于 2019-11-29 11:12:55
问题 Take this example: i = 0x12345678 print("{:08x}".format(i)) # shows 12345678 i = swap32(i) print("{:08x}".format(i)) # should print 78563412 What would be the swap32-function() ? Is there a way to byte-swap an int in python, ideally with built-in tools? 回答1: One method is to use the struct module: def swap32(i): return struct.unpack("<I", struct.pack(">I", i))[0] First you pack your integer into a binary format using one endianness, then you unpack it using the other (it doesn't even matter

Lua, dealing with non-ascii byte streams, byteorder change

半腔热情 提交于 2019-11-29 10:24:04
问题 Need to encode & decode byte-stream (containing non-ascii characters possibly), from/into uint16, uint32, uint64 (their typical C/C++ meaning), taking care of endianness. What is an efficient & hopefully cross-platform way to do such a thing in Lua ? My target arch is 64-bit x86_64, but would like to keep it portable (if it doesn't cost me on performance front). e.g. decode (say currently in a Lua string) -- 0x00, 0x1d, 0xff, 0x23, 0x44, 0x32 (little endian) as - uint16: (0x1d00) = 7424

Apple's heart rate monitoring example and byte order of bluetooth heart rate measurement characteristics

爷,独闯天下 提交于 2019-11-29 08:24:42
On the heart rate measurement characteristics: http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml EDIT Link is now at https://www.bluetooth.com/specifications/gatt/characteristics/ and look for "heart rate measurement". They no longer offer an XML viewer, but instead you need to view XML directly . Also for services it's on this page . END EDIT I want to make sure I'm reading it correctly. Does that actually says 5 fields? The mandatory, C1, C2, C3, and C4? And the mandatory is at the first byte, and C4

How to manage endianess of double from network

徘徊边缘 提交于 2019-11-29 07:47:18
I have a BIG problem with the answer to this question Swap bits in c++ for a double Yet, this question is more or less what I search for: I receive a double from the network and I want to encoded it properly in my machine. In the case I receive an int I perform this code using ntohl : int * piData = reinterpret_cast<int*>((void*)pData); //manage endianness of incomming network data unsigned long ulValue = ntohl(*piData); int iValue = static_cast<int>(ulValue); But in the case I receive an double , I don't know what to do. The answer to the question suggest to do: template <typename T> void

Is endian conversion required for wchar_t data?

老子叫甜甜 提交于 2019-11-29 07:04:33
In C/C++, if a multi-byte wide character (wchar_t) value is transmitted from a big-endian system to a little-endian system (or vice-versa), will it come out the same value on the other side? Or will the bytes need to be swapped? Yes you will need to swap them. The bytes will be retrieved from the transport in the same order they were put in. Just at the other end the ordering of these bytes has a different meaning. So you need to convert them to the correct endian-ness (is that a word?). The tried and true method is to convert to network byte order before transport. Then convert back to host

Portable serialisation of IEEE754 floating-point values

徘徊边缘 提交于 2019-11-29 06:11:14
I've recently been working on a system that needs to store and load large quantities of data, including single-precision floating-point values. I decided to standardise on network byte order for integers, and also decided to store floating point values in big-endian format, i.e.: |-- Byte 0 --| |-- Byte 1 -| Byte 2 Byte 3 # ####### # ####### ######## ######## Sign Exponent Mantissa 1b 8b, MSB first 23b, MSB first Ideally, I want to provide functions like htonl() and ntohl() , since I have already been using these for swabbing integers, and I also want to implement this in a way that has as