endianness

How to convert a Byte Array to an Int Array

泄露秘密 提交于 2019-11-26 07:45:27
问题 I am reading a file by using: int len = (int)(new File(args[0]).length()); FileInputStream fis = new FileInputStream(args[0]); byte buf[] = new byte[len]; fis.read(buf); As I found here. Is it possible to convert byte array buf to an Int Array ? Is converting the Byte Array to Int Array will take significantly more space ? Edit: my file contains millions of ints like, 100000000 200000000 ..... (written using normal int file wirte). I read it to byte buffer. Now I want to wrap it into

Does Java read integers in little endian or big endian?

筅森魡賤 提交于 2019-11-26 07:24:18
问题 I ask because I am sending a byte stream from a C process to Java. On the C side the 32 bit integer has the LSB is the first byte and MSB is the 4th byte. So my question is: On the Java side when we read the byte as it was sent from the C process, what is endian on the Java side? A follow-up question: If the endian on the Java side is not the same as the one sent, how can I convert between them? 回答1: Use the network byte order (big endian), which is the same as Java uses anyway. See man htons

Difference between Big Endian and little Endian Byte order

淺唱寂寞╮ 提交于 2019-11-26 06:15:50
问题 What is the difference between Big Endian and Little Endian Byte order ? Both of these seem to be related to Unicode and UTF16. Where exactly do we use this? 回答1: Big-Endian (BE) / Little-Endian (LE) are two ways to organize multi-byte words. For example, when using two bytes to represent a character in UTF-16, there are two ways to represent the character 0x1234 as a string of bytes (0x00-0xFF): Byte Index: 0 1 --------------------- Big-Endian: 12 34 Little-Endian: 34 12 In order to decide

Does bit-shift depend on endianness?

北战南征 提交于 2019-11-26 04:58:57
问题 Suppose I have the number \'numb\'=1025 [00000000 00000000 00000100 00000001] represented: On Little-Endian Machine: 00000001 00000100 00000000 00000000 On Big-Endian Machine: 00000000 00000000 00000100 00000001 Now, if I apply Left Shift on 10 bits (i.e.: numb <<= 10), I should have: [A] On Little-Endian Machine: As I noticed in GDB, Little Endian does the Left Shift in 3 steps: [I have shown \'3\' Steps to better understand the processing only] Treat the no. in Big-Endian Convention:

Why does optimisation kill this function?

痞子三分冷 提交于 2019-11-26 04:20:22
We recently had a lecture in university about programming specials in several languages. The lecturer wrote down the following function: inline u64 Swap_64(u64 x) { u64 tmp; (*(u32*)&tmp) = Swap_32(*(((u32*)&x)+1)); (*(((u32*)&tmp)+1)) = Swap_32(*(u32*) &x); return tmp; } While I totally understand that this is also really bad style in terms of readability, his main point was that this part of code worked fine in production code until they enabled a high optimization level. Then, the code would just do nothing. He said that all the assignments to the variable tmp would be optimized out by the

64 bit ntohl() in C++?

隐身守侯 提交于 2019-11-26 03:51:32
问题 The man pages for htonl() seem to suggest that you can only use it for up to 32 bit values. (In reality, ntohl() is defined for unsigned long, which on my platform is 32 bits. I suppose if the unsigned long were 8 bytes, it would work for 64 bit ints). My problem is that I need to convert 64 bit integers (in my case, this is an unsigned long long) from big endian to little endian. Right now, I need to do that specific conversion. But it would be even nicer if the function (like ntohl() )

Determining endianness at compile time

ぐ巨炮叔叔 提交于 2019-11-26 03:38:43
问题 Is there a safe, portable way to determine (during compile time) the endianness of the platform that my program is being compiled on? I\'m writing in C. [EDIT] Thanks for the answers, I decided to stick with the runtime solution! 回答1: This is for compile time checking You could use information from the boost header file endian.hpp, which covers many platforms. edit for runtime checking bool isLittleEndian() { short int number = 0x1; char *numPtr = (char*)&number; return (numPtr[0] == 1); }

Convert a byte array to integer in Java and vice versa

依然范特西╮ 提交于 2019-11-26 03:23:53
问题 I want to store some data into byte arrays in Java. Basically just numbers which can take up to 2 Bytes per number. I\'d like to know how I can convert an integer into a 2 byte long byte array and vice versa. I found a lot of solutions googling but most of them don\'t explain what happens in the code. There\'s a lot of shifting stuff I don\'t really understand so I would appreciate a basic explanation. 回答1: Use the classes found in the java.nio namespace, in particular, the ByteBuffer. It can

Bitwise operators and “endianness”

跟風遠走 提交于 2019-11-26 03:09:52
问题 Does endianness matter at all with the bitwise operations? Either logical or shifting? I\'m working on homework with regard to bitwise operators, and I can not make heads or tails on it, and I think I\'m getting quite hung up on the endianess. That is, I\'m using a little endian machine (like most are), but does this need to be considered or is it a wasted fact? In case it matters, I\'m using C. 回答1: Endianness only matters for layout of data in memory. As soon as data is loaded by the

C/C++: Force Bit Field Order and Alignment

五迷三道 提交于 2019-11-26 01:58:48
问题 I read that the order of bit fields within a struct is platform specific. What about if I use different compiler-specific packing options, will this guarantee data is stored in the proper order as they are written? For example: struct Message { unsigned int version : 3; unsigned int type : 1; unsigned int id : 5; unsigned int data : 6; } __attribute__ ((__packed__)); On an Intel processor with the GCC compiler, the fields were laid out in memory as they are shown. Message.version was the