endianness

Little Endian - Big Endian Problem

泪湿孤枕 提交于 2019-12-01 05:04:31
问题 Little Endian vs Big Endian Big Endian = 0x31014950 Little Endian = 0x50490131 However Using this Method inline unsigned int endian_swap(unsigned int& x) { return ( ( (x & 0x000000FF) << 24 ) | ( (x & 0x0000FF00) << 8 ) | ( (x & 0x00FF0000) >> 8 ) | ( (x & 0xFF000000) >> 24 ) ); } result = 0x54110131 i spent lot of time trying lots of similar methods and even a library one like unsigned long _byteswap_ulong(unsigned long value); But Still no luck .. all returns same result EDIT I'm Working on

In UTF-16, UTF-16BE, UTF-16LE, is the endian of UTF-16 the computer's endianness?

回眸只為那壹抹淺笑 提交于 2019-12-01 03:48:26
问题 UTF-16 is a two-byte character encoding. Exchanging the two bytes' addresses will produce UTF-16BE and UTF-16LE. But I find the name UTF-16 encoding exists in the Ubuntu gedit text editor, as well as UTF-16BE and UTF-16LE. With a C test program I found my computer is little endian, and UTF-16 is confirmed as same encoding of UTF-16LE. Also: There are two byte orders of a value (such as integer) in little/big endian computers. Little endian computers will produce little endian values in

Is it safe to detect endianess with union?

微笑、不失礼 提交于 2019-12-01 03:39:31
In other words, according to the C standard , is this code safe? (Assume uint8_t is one byte) void detectEndianness(void){ union { uint16_t w; uint8_t b; } a; a.w = 0x00FFU; if (a.b == 0xFFU) { puts("Little endian."); } else if (a.b == 0U) { puts("Big endian."); } else { puts("Stack Overflow endian."); } } What if I change it into this? Note the third if case that I'm aware of. a.w = 1U; if (a.b == 1U) { puts("Little endian."); } else if (a.b == 0U) { puts ("Big endian."); } else if (a.b == 0x80U) { /* Special potential */ } else { puts("Stack Overflow endian."); } Quoting from n1570: 6.5.2.3

Calculator to convert binary to float value — what am I doing wrong?

蹲街弑〆低调 提交于 2019-12-01 02:00:28
问题 I have the following code, which writes 6 floats to disk in binary form and reads them back: #include <iostream> #include <cstdio> int main() { int numSegs = 2; int numVars = 3; float * data = new float[numSegs * numVars]; for (int i = 0; i < numVars * numSegs; ++i) { data[i] = i * .23; std::cout << data[i] << std::endl; } FILE * handle = std::fopen("./sandbox.out", "wb"); long elementsWritten = std::fwrite(data, sizeof(float), numVars*numSegs, handle); if (elementsWritten != numVars*numSegs)

Read img medical image without header in matlab

我们两清 提交于 2019-12-01 01:38:56
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) However the resulting image is coming up as incorrect. Is there something that I am doing wrong ? Could

Is it safe to detect endianess with union?

拟墨画扇 提交于 2019-11-30 23:23:49
问题 In other words, according to the C standard , is this code safe? (Assume uint8_t is one byte) void detectEndianness(void){ union { uint16_t w; uint8_t b; } a; a.w = 0x00FFU; if (a.b == 0xFFU) { puts("Little endian."); } else if (a.b == 0U) { puts("Big endian."); } else { puts("Stack Overflow endian."); } } What if I change it into this? Note the third if case that I'm aware of. a.w = 1U; if (a.b == 1U) { puts("Little endian."); } else if (a.b == 0U) { puts ("Big endian."); } else if (a.b ==

How to read integers from a file that are 24bit and little endian using Python?

ぃ、小莉子 提交于 2019-11-30 22:24:06
问题 Is there an easy way to read these integers in? I'd prefer a built in method, but I assume it is possible to do with some bit operations. Cheers edit I thought of another way to do it that is different to the ways below and in my opinion is clearer. It pads with zeros at the other end, then shifts the result. No if required because shifting fills with whatever the msb is initially. struct.unpack('<i','\0'+ bytes)[0] >> 8 回答1: Python's struct module lets you interpret bytes as different kinds

How can I reorder the bytes of an integer?

此生再无相见时 提交于 2019-11-30 22:09:18
My task is to convert a data file from big endian to little endian & vice versa using C. I have been looking online for about 3 hours now for other examples and reading my text book, however I am so stuck on how to even start this function. So far I have the order of events correct (1 through 4) but inside my convert_and_save function do I have to create a char array using → char buffer[4]; ? Can someone please help me? even if you just give me clues on what to look up, I would greatly appreciate it. I need to write a function called: void convert_and_save(struct record item, FILE * output

Converting big-endian into little-endian and vice-versa in VBA

心已入冬 提交于 2019-11-30 19:38:20
问题 My machine is little-endian (Intel byte order). I need to read a binary file containing 16-bit signed integer data in Motorola/IEEE byte order ("big-endian"), then do some calculations, and finally write the resulting integer data in a big-endian binary file. How do I do the above in VBA, i.e. convert big-endian into little-endian and vice-versa? The reason is, I'm processing NASA Shuttle Radar Topography Mission data (HGT file format). 回答1: Here is a subroutine that may get you started:

MATLAB convert big-endian order bytes into floating point values

ぃ、小莉子 提交于 2019-11-30 19:24:42
问题 I have the following bytes stored in a vector: data = [189 33 136 147] These 4 bytes represent a single float in Big-endian order. How can I get this number in MATLAB? I will need to concatenate and convert. I tried: x = typecast(str2num(sprintf('%d%d%d%d',data(1),data(2),data(3),data(4))), 'single') To no avail (I got x = [] ). 回答1: great example here: >> dataL = typecast(uint8([189, 33, 136, 147]), 'uint32') dataL = 2475172285 >> dataF = double(dataL) dataF = 2.4752e+09 big to little, try