How can I reverse the byte order of an NSInteger or NSUInteger in objective-c
Asked Answered
D

1

6

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.

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).

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
Disconsider answered 30/9, 2010 at 13:17 Comment(0)
I
16

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.

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.

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.

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.

Interatomic answered 30/9, 2010 at 13:41 Comment(1)
Thanks I had looked around quite a bit and not found NSSwapBigIntToHost. I ended up doing some lower bit manipulation like you suggested. I am making a bit of an assumption that my ints will be 32-bit in my code. What I ended up doing was creating a unsigned char tref = (unsigned char)&temp; I then loop through and swap the bytes, then cast the tref to an integer. Not pretty but works as long as I'm at 32-bit.Disconsider

© 2022 - 2024 — McMap. All rights reserved.