endianness

What's a portable way of converting Byte-Order of strings in C

佐手、 提交于 2019-12-09 22:35:23
问题 I am trying to write server that will communicate with any standard client that can make socket connections (e.g. telnet client) It started out as an echo server, which of course did not need to worry about network byte ordering. I am familiar with ntohs, ntohl, htons, htonl functions. These would be great by themselves if I were transfering either 16 or 32-bit ints, or if the characters in the string being sent were multiples of 2 or 4 bytes. I'd like create a function that operates on

Converting 4 bytes in little endian order into an unsigned integer

ε祈祈猫儿з 提交于 2019-12-09 06:54:57
问题 I have a string of 256*4 bytes of data. These 256* 4 bytes need to be converted into 256 unsigned integers. The order in which they come is little endian, i.e. the first four bytes in the string are the little endian representation of the first integer, the next 4 bytes are the little endian representation of the next integer, and so on. What is the best way to parse through this data and merge these bytes into unsigned integers? I know I have to use bitshift operators but I don't know in

Determine the endianness of a numpy array

白昼怎懂夜的黑 提交于 2019-12-09 03:38:38
问题 I have a numpy.array and I want to find out what endianness is used in the underlying representation. A byteorder property is documented here, but none of the given examples show it being used with an array. 回答1: byteorder is a data type objects dtype attribute so you need to do this: In [10]: import numpy as np In [11]: arr = np.array([1,2,3]) In [12]: arr.dtype.byteorder Out[12]: '=' 来源: https://stackoverflow.com/questions/34097845/determine-the-endianness-of-a-numpy-array

The reason behind endianness?

六月ゝ 毕业季﹏ 提交于 2019-12-08 22:44:25
问题 So, I was wondering, why some architectures use little-endian and others big-endian. I remember I read somewhere that it has to do with performance, however, I don't understand how can endianness influence it. Also I know that: The little-endian system has the property that the same value can be read from memory at different lengths without using different addresses. Which seems a nice feature, but, even so, many systems use big-endian, which probably means big-endian has some advantages too

When to worry about endianness?

荒凉一梦 提交于 2019-12-08 16:39:20
问题 I have seen countless references about endianness and what it means. I got no problems about that... However, my coding project is a simple game to run on linux and windows, on standard "gamer" hardware. Do I need to worry about endianness in this case? When should I need to worry about it? My code is simple C and SDL+GL, the only complex data are basic media files (png+wav+xm) and the game data is mostly strings, integer booleans (for flags and such) and static-sized arrays. So far no user

Convert uint64_t to byte array portably and optimally in Clang

拟墨画扇 提交于 2019-12-08 15:11:54
问题 If you want to convert uint64_t to a uint8_t[8] (little endian). On a little endian architecture you can just do an ugly reinterpret_cast<> or memcpy() , e.g: void from_memcpy(const std::uint64_t &x, uint8_t* bytes) { std::memcpy(bytes, &x, sizeof(x)); } This generates efficient assembly: mov rax, qword ptr [rdi] mov qword ptr [rsi], rax ret However it is not portable. It will have different behaviour on a little endian machine. For converting uint8_t[8] to uint64_t there is a great solution

Converting 8 bytes of little-endian binary into a double precision float

拟墨画扇 提交于 2019-12-08 15:06:55
问题 I have a binary file that I read byte by byte. I come across a section that is 8 bytes long, holding a double precision float (little endian). I can't figure out how to read this in and calculate it properly with masking and/or casting. (To be specific, the file type is .LAS, but that shouldn't matter). Are there any Java tricks? 回答1: You can use ByteBuffer from a byte[] bytes double d = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN ).getDouble(); from a Socket ByteBuffer bb =

Bit ordering in a byte, does it matter?

假装没事ソ 提交于 2019-12-08 12:18:02
问题 Let's say we have two machines on a network MA and MB , MA considers little endian the order of the bits in a byte, on the contrary MB cosiders big endian the order of the bits in a byte. How do MA and MB agree on what "endianess" to use for the bits in a byte during the communication over the network ? Is there a standard "network endianess" or what ? Do socket programmers have to take any actions in ensuring a correct communication ? For example HTTP is a text protocol, that means that

C, Little and Big Endian confusion

狂风中的少年 提交于 2019-12-08 10:43:37
问题 I try to understand C programming memory Bytes order, but I'm confuse. I try my app with some value on this site for my output verification : www.yolinux.com/TUTORIALS/Endian-Byte-Order.html For the 64bits value I use in my C program: volatile long long ll = (long long)1099511892096; __mingw_printf("\tlong long, %u Bytes, %u bits,\t%lld to %lli, %lli, 0x%016llX\n", sizeof(long long), sizeof(long long)*8, LLONG_MIN, LLONG_MAX , ll, ll); void printBits(size_t const size, void const * const ptr)

endianness theory and concept

泪湿孤枕 提交于 2019-12-08 02:04:33
问题 This isn't a question specific to any programming language. Say you have some file written on a big-endian machine, and you know this. If two single-byte values were written back-to-back, how would you know? Big-endian reverses the order of 16, 32, and 64 bit values, so how would you know you need to read it as individual bytes? For instance, you write the byte 0x11, then the byte 0x22. The file then contains 0x1122. If you read that on a little endian machine, you'd have to convert it. So