如何在Objective-C中反转NSInteger或NSUInteger的字节顺序 [英] How can I reverse the byte order of an NSInteger or NSUInteger in objective-c

查看:295
本文介绍了如何在Objective-C中反转NSInteger或NSUInteger的字节顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对的后续行动,但有一个不同的问题,所以我觉得我应该在单独的话题中提问.

This is a somewhat of a follow up to this posting but with a different question so I felt I should ask in a separate thread.

我正要从文件中读取的内存中有四个连续字节.我想将它们存储为一个位数组(它们的实际int值直到以后都没有关系).当我打印出int中的内容时,我注意到它似乎是以相反的顺序存储的(小尾数).

I am at the point where I have four consecutive bytes in memory that I have read in from a file. I'd like to store these as a bit array (the actual int value of them does not matter until later). When I print out what is in my int, I notice that it seems to be stored in reverse order (little endian).

有没有人有一个很好的方法来反转字节顺序.然后一旦反转,挑选出跨越两个字节的连续位并转换回int?

Does anyone have a good method for reversing the order of the bytes. Then once reversed, picking out consecutive bits spanning two bytes and converting back to an int?

unsigned char data[] = { 0x00, 0x02, 0x45, 0x28 };
NSInteger intData = *((NSInteger *)data);

NSLog(@"data:%08x", intData); // data:28450200

推荐答案

可可粉(确切地说是Foundation框架)具有交换字节的字节序的功能:NSSwapIntNSSwapShortNSSwapLongNSSwapLongLong.无论如何,它们都会围绕字节交换-它们由小端整数组成大端整数,反之亦然.

Cocoa (or to be exact the Foundation framework) has functions to swap the endianness of bytes: NSSwapInt, NSSwapShort, NSSwapLong, and NSSwapLongLong. These swap around the bytes no matter what - they make big-endian integers from small-endian integers and vice versa.

如果您知道哪种格式,则可以使用其他功能将其交换为本地字节序:NSSwapLittleIntToHostNSSwapBigIntToHost.还有从原始格式转换为小端或大端格式的反向函数:NSSwapHostIntToLittleNSSwapHostIntToBig.这些也可用于其他整数类型和浮点类型.它们的作用是在必要时在值上调用原始交换函数.因此,NSSwapLittleIntToHost不会执行任何操作,而NSSwapBigIntToHost会在小字节序的计算机上返回NSSwapInt的结果.

If you know which format you have there are other functions that swap it to the native endianness: NSSwapLittleIntToHost and NSSwapBigIntToHost. There are also the reverse functions which swap from the native format to little or big endian format: NSSwapHostIntToLittle and NSSwapHostIntToBig. Those are available for the other integer types and floating point types as well. What they do is they call the primitive swap functions on the values if necessary. So NSSwapLittleIntToHost doesn’t do anything while NSSwapBigIntToHost returns the result of NSSwapInt on a little endian machine.

请注意,这些参数采用编译器整数类型的参数,而不是NSInteger类型的参数.因此,如果您使用的是NSInteger,则根据生成的是32位还是64位代码,必须使用不同的功能.

Note that these take parameters of the compilers integer types and not the NSInteger type. So depending on wether you’re generating 32bit or 64bit code you have to use different functions if you are using NSInteger.

您也不应该将字节数组转换为整数指针并对其取消引用.最好使用移位操作来组装整数.仅当NSInteger为32位宽时,您的代码才能工作.如果是64位,那么您的电话号码将是垃圾,否则您的程序甚至可能崩溃.但是,即使您使用始终为32位宽的整数类型(例如,从C99 <stdint.h>标头中的int32_t),也可能无法按预期工作.

You also should not cast your byte array to an integer pointer and dereference that. It would be better to assemble the integer using bit shift operations. Your code will only work if NSInteger is 32 bit wide. If it is 64 bit then your number will be garbage or your program might even crash. But even if you are using an integer type that is always 32 bit wide (int32_t from the C99 <stdint.h> header for example) this might not work as expected.

这篇关于如何在Objective-C中反转NSInteger或NSUInteger的字节顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆