endianness

Simple bitwise manipulation for little-endian integer, in big-endian machine?

这一生的挚爱 提交于 2019-12-30 10:07:06
问题 For a specific need I am building a four byte integer out of four one byte chars, using nothing too special (on my little endian platform): return (( v1 << 24) | (v2 << 16) | (v3 << 8) | v4); I am aware that an integer stored in a big endian machine would look like AB BC CD DE instead of DE CD BC AB of little endianness, although would it affect the my operation completely in that I will be shifting incorrectly, or will it just cause a correct result that is stored in reverse and needs to be

Any way to read big endian data with little endian program?

六眼飞鱼酱① 提交于 2019-12-30 09:56:27
问题 An external group provides me with a file written on a Big Endian machine, and they also provide a C++ parser for the file format. I only can run the parser on a little endian machine - is there any way to read the file using their parser without add a swapbytes() call after each read? 回答1: Back in the early Iron Age, the Ancients encountered this issue when they tried to network primitive PDP-11 minicomputers with other primitive computers. The PDP-11 was the first little-Endian computer,

PNG file format endianness?

安稳与你 提交于 2019-12-29 08:26:23
问题 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? 回答1: Yes, according to the specification, integers must be in network byte order (big endian): All

How to manage endianess of double from network

笑着哭i 提交于 2019-12-29 07:11:48
问题 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

Signed Integer Network and Host Conversion

本小妞迷上赌 提交于 2019-12-29 06:17:35
问题 I would like to convert a int32_t from host byte order to network byte order and vice versa. I know about the htonl() function and its variants, but this takes unsigned integers. Is there a standard library function which can do the same with signed integers or do I have to implement it myself? And if I have to implement it myself, how should I do it? I'm looking to find a routine that will work on Linux and Mac OS X. 回答1: It does not matter. htonl is concerned with bytes, not with

constexpr and endianness

别等时光非礼了梦想. 提交于 2019-12-29 03:34:10
问题 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

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

徘徊边缘 提交于 2019-12-29 03:29:08
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Detecting endianness programmatically in a C++ program Is there any library function available to find the endian-ness of my PC? 回答1: 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"); } 回答2: I'm not aware of a library function. You can get the address of an integer, then treat that address as a character

Detecting Endianness

僤鯓⒐⒋嵵緔 提交于 2019-12-28 02:53:31
问题 I'm currently trying to create a C source code which properly handles I/O whatever the endianness of the target system. I've selected "little endian" as my I/O convention, which means that, for big endian CPU, I need to convert data while writing or reading. Conversion is not the issue. The problem I face is to detect endianness, preferably at compile time (since CPU do not change endianness in the middle of execution...). Up to now, I've been using this : #if __BYTE_ORDER__ == __ORDER_LITTLE

Qt parse string of undefined size from a binary data stream

与世无争的帅哥 提交于 2019-12-25 08:57:27
问题 I have a binary data stream which contains data that should be interpreted as a Qstring. Starting from the third byte. Here is how the package is generated (on a client). QByteArray package; package.append( QByteArray::fromHex("0002") ); // First two bytes package.append( "filename.txt" ); // String of undefined size package.append( QByteArray::fromHex("00")); // End of string The decoding is done on a different machine (server). I would like to get a Qstring of value "filename.txt" from the

Strings in BigEndianStructure

微笑、不失礼 提交于 2019-12-25 07:42:28
问题 I want to do something like that: from ctypes import * class Packet(BigEndianStructure): _fields_ = [("length", c_ushort), ("session", c_uint), ("command", c_ushort)] class PacketString(BigEndianStructure): _fields_ = [("length", c_ushort), ("value", c_char_p)] class InitialPacket(Packet): _fields_ = [("time", PacketString)] However I getting error because c_char_p can only be in native byte order. But maybe there is some other way how I can make strings whose length is specified right before