Is there a smart way to display iPhone OSStatus error values?

Many / most OSStatus values ​​on iPhone are 4-character display values ​​stuck in a 4-byte int. In order for the values ​​to be displayed in a hex dump in a system with small bytes, the bytes are in the reverse order in memory, so just copying them to the buffer with memcpy and marking zero at the end does not lead to the desired result.

Does anyone have a smart way to swap bytes around and type them in a character string in a relatively small number (source code) of keystrokes?

+4
source share
2 answers

Wanting to minimize the amount of coding I need (since this is the diagnostic / debugging code that I want to keep simple), I chose the C function, which returns an NSString:

NSString* statToString(UInt32 source) { char buf[4]; char* sourceView = (char*) &source; buf[0] = sourceView[3]; buf[1] = sourceView[2]; buf[2] = sourceView[1]; buf[3] = sourceView[0]; return [[[NSString alloc] initWithBytes:buf length:4 encoding:NSUTF8StringEncoding] autorelease]; } 

This allows you to use the function directly in the NSLog parameter list, for example, without the need to declare any tempo.

Perhaps he can tighten it using one of the tampon options, but this level of optimization is not required.

+2
source

Check out the functions in CFByteOrder.h, for example CFSwapInt32 (uint32_t).

If this is not available, just write your own 32-bit byte code, for example:

 #define SWAPUINT32(x) (((x & 0xff) << 24) | ((x & 0xff00) << 8) | \ ((x & 0xff0000) >> 8) | ((x & 0xff000000) >> 24)) 

and use it this way:

 uint32_t swappedValue = SWAPUINT32(originalValue); 
+2
source

All Articles