且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS版/ C:转换"整数QUOT;到四个字符的字符串

更新时间:2023-02-04 22:53:09

你在谈论的类型是 FourChar code 在 CFBase.h 。它相当于一个 OSTYPE 。最简单的方式之间的转换 OSTYPE 的NSString 使用 NSFileTypeForHFSType code() NSHFSType codeFromFileType()。这些功能,遗憾的是,不可用在iOS上。

The type you're talking about is a FourCharCode, defined in CFBase.h. It's equivalent to an OSType. The easiest way to convert between OSType and NSString is using NSFileTypeForHFSTypeCode() and NSHFSTypeCodeFromFileType(). These functions, unfortunately, aren't available on iOS.

有关iOS和可可便携code,我喜欢约阿希姆·本特松的 FourCC2Str()从他 NCCommon.h (加为方便使用一个小铸造清理):

For iOS and Cocoa-portable code, I like Joachim Bengtsson's FourCC2Str() from his NCCommon.h (plus a little casting cleanup for easier use):

#include <TargetConditionals.h>
#if TARGET_RT_BIG_ENDIAN
#   define FourCC2Str(fourcc) (const char[]){*((char*)&fourcc), *(((char*)&fourcc)+1), *(((char*)&fourcc)+2), *(((char*)&fourcc)+3),0}
#else
#   define FourCC2Str(fourcc) (const char[]){*(((char*)&fourcc)+3), *(((char*)&fourcc)+2), *(((char*)&fourcc)+1), *(((char*)&fourcc)+0),0}
#endif

FourCharCode code = 'APPL';
NSLog(@"%s", FourCC2Str(code));
NSLog(@"%@", @(FourCC2Str(code));

您当然可以扔 @()进入宏更容易使用。

You could of course throw the @() into the macro for even easier use.