endianness

converting bits in to integer

风流意气都作罢 提交于 2019-12-20 04:10:02
问题 I receive a datapacket containing a byte array and I have to get some integer values from it. Here is a part of the documentation. Can someone help me please? This comes in a 4-byte array. Year from 1990 to 2052 (6 bit), Month from 1 to 12 (4 bit), Day from 1 to 31 (5 bit), Hour from 0 to 23 (5 bit), Minute from 0 to 59 (6 bit), Second from 0 to 59 (6 bit) Default value: 1 January 2000, 12:00:00 The format of the message is in little endian. 回答1: What you need is some bitwise operations.

In Java, when writing to a file with DataOutputStream, how do I define the Endian of the data being written?

白昼怎懂夜的黑 提交于 2019-12-19 15:05:09
问题 I'm using DataOutputStream to write to a file, however I want to change the endian of the data. This is how i'm writing the byte data to the file (it outputs in Little endian by default) public void generateBinObjFile(String outputFile) try { // Create file DataOutputStream stream = new DataOutputStream( new FileOutputStream(outputFile)); stream.writeShort(this.quantize(this.xComponents.get(index), //<-- Short is written in little Endian this.min_x, this.max_x) - 32768); } // catch statements

What is the C Equivalent of Python's pack(“<I”, 0)

笑着哭i 提交于 2019-12-19 10:15:10
问题 I don't know much python but from what I can tell from the documentation the code: str = "AAAA" str += pack("<I", 0) would append the result of the pack function to str, which would be the integer value of 0 in little-endian style. My question is what the C equivalent of this would be. Would it just be: char str[20] = "AAAA"; strcat(str, "\x00"); ?... 回答1: strcat() stops at the first NUL, so no. char str[20] = "AAAA"; int val = 0; int nval = htole32(val); memcpy(str + 4, (char*)&nval, 4); 来源:

Read img medical image without header in matlab

喜夏-厌秋 提交于 2019-12-19 05:04:58
问题 I have a radiograph .img file without the header file. However, the researchers who have published the file have given this information about it High resolution (2048 x 2048 matrix size, 0.175mm pixel size) Wide density range (12bit, 4096 gray scale) Universal image format (no header, big-endian raw data) Using this information, I tried fread command in Matlab to read the image into Matlab. fid = fopen('image.img','r','B'); oneSlice = fread(fid, [2048 2048], '*uint8','B'); imshow(oneSlice)

How bit endianness affects bitwise shifts and file IO in C?

那年仲夏 提交于 2019-12-18 16:59:21
问题 Let L and B be two machines. L order its bits from LSB (Least Significant Bit) to MSB (Most Significant Bit) while B order from MSB to LSB. Or, in other words, L uses Little Endian while B uses Big Endian bit - not to be confused with byte - ordering. Problem 1 SOLVED : We are writing the following code which we want to be portable: #include <stdio.h> int main() { unsigned char a = 1; a <<= 1; printf("a = %d\n", (int) a); return 0; } on L , it will print 2, but what happens on B ? Will it

How bit endianness affects bitwise shifts and file IO in C?

ぃ、小莉子 提交于 2019-12-18 16:59:14
问题 Let L and B be two machines. L order its bits from LSB (Least Significant Bit) to MSB (Most Significant Bit) while B order from MSB to LSB. Or, in other words, L uses Little Endian while B uses Big Endian bit - not to be confused with byte - ordering. Problem 1 SOLVED : We are writing the following code which we want to be portable: #include <stdio.h> int main() { unsigned char a = 1; a <<= 1; printf("a = %d\n", (int) a); return 0; } on L , it will print 2, but what happens on B ? Will it

How does JPEG endianness matter on coding?

陌路散爱 提交于 2019-12-18 13:20:57
问题 I'm currently working on a big project that involve pictures. One of the big issues I'm having is with the endianness of the picture (jpeg to be clearer). I always though that in our modern world we didn't have to botter about this subject, but now I'm not sure. What I do: I do an HTTP request to an IP camera, the camera return me an array of byte. I parse these byte into an object Image in .NET using Image.FromStream. I take my Image object and do a Save to a physical file on the hard disk.

Efficient way to read big endian data in C#

断了今生、忘了曾经 提交于 2019-12-18 13:13:14
问题 I use the following code to read BigEndian information using BinaryReader but I'm not sure if it is the efficient way of doing it. Is there any better solution? Here is my code: // some code to initialize the stream value // set the length value to the Int32 size BinaryReader reader =new BinaryReader(stream); byte[] bytes = reader.ReadBytes(length); Array.Reverse(bytes); int result = System.BitConverter.ToInt32(temp, 0); 回答1: BitConverter.ToInt32 isn't very fast in the first place. I'd simply

How to convert 32 bit integer to network byte order?

无人久伴 提交于 2019-12-18 09:42:14
问题 How to convert the 32 bit integer to network byte order. What is the right way to do that? [1024].pack("N") OR [1,0,2,4].pack("N") Thanks 回答1: To start, look at the output of each: >> [1024].pack("N") => "\000\000\004\000" >> [1,0,2,4].pack("N") => "\000\000\000\001" Note what the second is missing: >> [1,0,2,4].pack("NNNN") => "\000\000\000\001\000\000\000\000\000\000\000\002\000\000\000\004" 来源: https://stackoverflow.com/questions/12571715/how-to-convert-32-bit-integer-to-network-byte-order

when to use hton/ntoh and when to convert data myself?

人盡茶涼 提交于 2019-12-18 09:04:55
问题 to convert a byte array from another machine which is big-endian, we can use: long long convert(unsigned char data[]) { long long res; res = 0; for( int i=0;i < DATA_SIZE; ++i) res = (res << 8) + data[i]; return res; } if another machine is little-endian, we can use long long convert(unsigned char data[]) { long long res; res = 0; for( int i=DATA_SIZE-1;i >=0 ; --i) res = (res << 8) + data[i]; return res; } why do we need the above functions? shouldn't we use hton at sender and ntoh when