endianness

Unexpected behavior when printing 4-byte integer byte by byte

余生颓废 提交于 2020-01-03 01:28:46
问题 I have this sample code for converting 32 bit integers to ip addresses. #include <stdio.h> int main() { unsigned int c ; unsigned char* cptr = (unsigned char*)&c ; while(1) { scanf("%d",&c) ; printf("Integer value: %u\n",c); printf("%u.%u.%u.%u \n",*cptr, *(cptr+1), *(cptr+2), *(cptr+3) ); } } This code gives incorrect output for input 2249459722 . But when i replace scanf("%d",&c) ; by scanf("%u",&c) ; The output comes out to be correct. P.S : I know about inet_ntop and inet_pton . I expect

How to determine the endian mode the processor is running in?

試著忘記壹切 提交于 2020-01-02 05:26:07
问题 How do I determine the endian mode the ARM processor is running in using only assembly language. I can easily see the Thumb/ARM state reading bit 5 of the CPSR, but I don't know if there a corresponding bit in the CPSR or elsewhere for endianness. ;silly example trying to execute ARM code when I may be in Thumb mode.... MRS R0,CPSR ANDS R0,#0x20 BNE ThumbModeIsActive B ARMModeIsActive I've got access to the ARM7TDMI data sheet, but this document does not tell me how to read the current state.

When is htonl(x) != ntohl(x) ? (Or when is converting to and from Network Byte Order not equivalent on the same machine?)

帅比萌擦擦* 提交于 2020-01-02 01:22:07
问题 In regards to htonl and ntohl. When would either of these two lines of code evaluate to false. htonl(x) == ntohl(x); htonl(ntohl(x)) == htonl(htonl(x)); In other words, when are these two operations not equivalent on the same machine ? The only scenario I can think of is a machine that does not work on 2's complement for representing integers. Is the reason largely historical, for coding clarity, or for something else? Do any modern architectures or environments exists today where these

Union and endianness

有些话、适合烂在心里 提交于 2020-01-01 10:02:18
问题 typedef union status { int nri; char cit[2]; }Status; int main() { Status s; s.nri = 1; printf("%d \n",s.nri); printf("%d,%d,\n",s.cit[0],s.cit[1]); } OUTPUT: 1 0,1 I know this output on the second line is depend on the endianess of the CPU. How I can write such in a platform-independant program? Is there any way of checking the endianess of the CPU? 回答1: You can use htonl() and/or ntohl(). htonl() stands for "host to network long", while ntohl() stands for "network to host long". The "host"

Union and endianness

家住魔仙堡 提交于 2020-01-01 10:02:10
问题 typedef union status { int nri; char cit[2]; }Status; int main() { Status s; s.nri = 1; printf("%d \n",s.nri); printf("%d,%d,\n",s.cit[0],s.cit[1]); } OUTPUT: 1 0,1 I know this output on the second line is depend on the endianess of the CPU. How I can write such in a platform-independant program? Is there any way of checking the endianess of the CPU? 回答1: You can use htonl() and/or ntohl(). htonl() stands for "host to network long", while ntohl() stands for "network to host long". The "host"

Faster way to swap endianness in C# with 32 bit words

人盡茶涼 提交于 2020-01-01 08:45:44
问题 In this question, the following code: public static void Swap(byte[] data) { for (int i = 0; i < data.Length; i += 2) { byte b = data[i]; data[i] = data[i + 1]; data[i + 1] = b; } } was rewritten in unsafe code to improve its performance: public static unsafe void SwapX2(Byte[] Source) { fixed (Byte* pSource = &Source[0]) { Byte* bp = pSource; Byte* bp_stop = bp + Source.Length; while (bp < bp_stop) { *(UInt16*)bp = (UInt16)(*bp << 8 | *(bp + 1)); bp += 2; } } } Assuming that one wanted to do

ByteBuffer Little Endian insert not working

こ雲淡風輕ζ 提交于 2020-01-01 07:34:51
问题 I have to make a two way communication between a legacy system and an android device. The legacy system uses little endian byte ordering. I have successfully implemented the receiving part, however sending not works. Strange because for me it seems that the ByteBuffer class malfunctions (I can hardly believe that) ByteBuffer byteBuffer = ByteBuffer.allocate(4); byteBuffer.order(ByteOrder.LITTLE_ENDIAN); byteBuffer = ByteBuffer.allocate(4); byteBuffer.putInt(88); byte[] result = byteBuffer

ByteBuffer Little Endian insert not working

夙愿已清 提交于 2020-01-01 07:34:04
问题 I have to make a two way communication between a legacy system and an android device. The legacy system uses little endian byte ordering. I have successfully implemented the receiving part, however sending not works. Strange because for me it seems that the ByteBuffer class malfunctions (I can hardly believe that) ByteBuffer byteBuffer = ByteBuffer.allocate(4); byteBuffer.order(ByteOrder.LITTLE_ENDIAN); byteBuffer = ByteBuffer.allocate(4); byteBuffer.putInt(88); byte[] result = byteBuffer

Getting pixel format from CGImage

佐手、 提交于 2019-12-31 22:27:15
问题 I understand bitmap layout and pixel format subject pretty well, but getting an issue when working with png / jpeg images loaded through NSImage – I can't figure out if what I get is the intended behaviour or a bug. let nsImage:NSImage = NSImage(byReferencingURL: …) let cgImage:CGImage = nsImage.CGImageForProposedRect(nil, context: nil, hints: nil)! let bitmapInfo:CGBitmapInfo = CGImageGetBitmapInfo(cgImage) Swift.print(bitmapInfo.contains(CGBitmapInfo.ByteOrderDefault)) // True My

Endianness — why do chars put in an Int16 print backwards?

心不动则不痛 提交于 2019-12-31 03:55:09
问题 The following C code, compiled and run in XCode: UInt16 chars = 'ab'; printf("\nchars: %2.2s", (char*)&chars); prints 'ba', rather than 'ab'. Why? 回答1: That particular implementation seems to store multi-character constants in little-endian format. In the constant 'ab' the character 'b' is the least significant byte (the little end) and the character 'a' is the most significant byte. If you viewed chars as an array, it'd be chars[0] = 'b' and chars[1] = 'a' , and thus would be treated by