iOS版/ C:转换"整数QUOT;到四个字符的字符串 [英] iOS/C: Convert "integer" into four character string
问题描述
很多与音频会话编程相关的常量是真正的四字符串(<一个href=\"http://developer.apple.com/library/ios/documentation/AudioToolbox/Reference/AudioSessionServicesReference/Reference/reference.html#//apple_ref/doc/uid/TP40007915-CH6-SW2\"相对=nofollow>音频会话服务参考)。这同样适用于在<一个href=\"http://developer.apple.com/library/ios/documentation/AudioToolbox/Reference/AudioSessionServicesReference/Reference/reference.html#//apple_ref/doc/uid/TP40007915-CH7a-SW1\"相对=nofollow> OSStatus code 从函数返回象 AudioSessionGetProperty
。
A lot of the constants associated with Audio Session programming are really four-character strings (Audio Session Services Reference). The same applies to the OSStatus code returned from functions like AudioSessionGetProperty
.
问题是,当我尝试打印这些东西开箱,它们看起来像1919902568.我可以把它插入到计算机并打开ASCII输出,它会告诉我蓉城,但必须有一个编程的方式来做到这一点。
The problem is that when I try to print these things out of the box, they look like 1919902568. I can plug that into Calculator and turn on ASCII output and it'll tell me "roch", but there must be a programmatic way to do this.
我已经有限的成功具有以下块我的C功能之一:
I've had limited success in one of my C functions with the following block:
char str[20];
// see if it appears to be a four-character code
*(UInt32 *) (str + 1) = CFSwapInt32HostToBig(error);
if (isprint(str[1]) && isprint(str[2]) && isprint(str[3]) && isprint(str[4])) {
str[0] = str[5] = '\'';
str[6] = '\0';
} else {
// no, format as integer
sprintf(str, "%d", (int)error);
}
我想要做的是抽象的这个功能摆脱目前的功能,以便在其他地方使用它。我试图做
What I want to do is to abstract this feature out of its current function, in order to use it elsewhere. I tried doing
char * fourCharCode(UInt32 code) {
// block
}
void someOtherFunction(UInt32 foo){
printf("%s\n",fourCharCode(foo));
}
但给了我A *€/3íT:E *€/ +€/,而不是蓉城。我的C福不是很强,但我的直觉是,上述code试图间preT的内存地址为一个字符串。或者,也许有一个编码问题?任何想法?
but that gives me "à*€/3íT:ê*€/+€/", not "roch". My C fu isn't very strong, but my hunch is that the above code tries to interpret the memory address as a string. Or perhaps there's an encoding issue? Any ideas?
推荐答案
你在谈论的类型是 FourChar code
在<$ C定义$ C> 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.
这篇关于iOS版/ C:转换&QUOT;整数QUOT;到四个字符的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!