如何将NSString转换为char [英] How to convert an NSString to char
问题描述
我试图将 NSString
转换为 ResType
,如下面 MacTypes.h
。
I am trying to convert an NSString
to a ResType
, as defined below in MacTypes.h
.
FourCharCode // A 32-bit value made by packing four 1 byte characters together
typedef FourCharCode ResType;
我想我可以使用 [aString getCharacters:range:] $
I think I could use [aString getCharacters:range:]
, but is there a more straight forward way to make this conversion?
在尝试过大卫的建议之后,这里有一些更详细的信息。
After trying suggestions from David, here is some further information.
我使用GTResourceFork,一个Cocoa包装器访问资源分叉。我调用的方法是:
- (NSArray *)usedResourcesOfType:(ResType)type;
I am using GTResourceFork, a Cocoa wrapper for accessing resource forks. The method I am calling is: - (NSArray *)usedResourcesOfType: (ResType)type;
如果我硬编码'RTF'得到我期望的结果。我不知道如何将包含RTF的NSString转换为硬编码的值。我创建了一个测试用例使用NSString的getCharacters和getBytes,他们都给我不同的整数值。如何转换NSString给我一个与硬编码的整数值相同的整数值?
If I hard code a value of 'RTF ', I get the results I expect. I cannot figure out how to convert an NSString containing "RTF " to the hard-coded value. I created a test case using NSString's getCharacters and getBytes, and they all give me different integer values. How can I convert the NSString to give me the same integer value as the hard-coded one?
Method used: Value: Casted int value:
Hard Coded(Works): 'RTF ' '1381254688'
getCharacters: 'RTF ' '5505106'
getBytes(ASCII): 'RTF ' '541480018'
getBytes(UTF8): 'RTF ' '541480018'
提前感谢
Lance
Thanks in advance, Lance
推荐答案
getCharacters:range:
的问题是它给你的UTF -16个字符( unichar
s),而您想要ASCII。
The problem with getCharacters:range:
is that it gives you UTF-16 characters (unichar
s), whereas you want ASCII.
* *)[aString UTF8String]
会将字符串转换为UTF-8(这相当于ASCII,只要字符串中的所有字符都符合ASCII范围),然后给你前四个字节作为 ResType
值。
*(ResType*)[aString UTF8String]
will convert the string to UTF-8 (which is equivalent to ASCII as long as all the characters in the string fit within the ASCII range) and then give you the first four bytes as a ResType
value. Whether this is efficient enough depends on how often you want to do these conversions.
另一个选择是使用 getBytes:maxLength:usedLength:encoding:选项:range:remainingRange:
,编码设置为 NSASCIIStringEncoding
或 NSUTF8StringEncoding
缓冲区设置为指向现有 ResType
变量的指针,最大长度设置为 4
(或 sizeof(ResType)
)。
Another option is to use getBytes:maxLength:usedLength:encoding:options:range:remainingRange:
with the encoding set to NSASCIIStringEncoding
or NSUTF8StringEncoding
, the destination buffer set to a pointer to an existing ResType
variable, and the maximum length set to 4
(or sizeof (ResType)
).
我想出了为什么你没有得到我的建议的正确结果。事实证明,在四字符整数文字中,字节存储的方式与它们的写法相反。这里有一个例子:
I've figured out why you're not getting the correct result with my suggestion. It turns out that in four-character integer literals, the bytes are stored in the opposite order to how they're written. Here's an example:
#include <Foundation/Foundation.h>
int main() {
int code = 'RTF ';
printf("'%c%c%c%c' = %d\n", ((char*)&code)[0], ((char*)&code)[1],
((char*)&code)[2], ((char*)&code)[3],
code);
}
输出'FTR'= 1381254688
。所以,如果你想从
NSString
s转换为这些值,这里有几个选项:
The output is ' FTR' = 1381254688
. So, if you want to convert from NSString
s to these values, here are a few options:
- 将字符串复制到一个四字节缓冲区(使用我建议的方法之一),然后通过交换字节
0
与字节3
,以及字节1
与字节2
$ b - 同样,但是使用标准的endianness-swapping算法(如 c> 使用
characterAtIndex:
迭代第一个并将它们反向插入一个四字节缓冲区。请记住, characterAtIndex:
返回一个UTF-16字符,但这可以很容易地转换为ASCII字符,假设它在ASCII范围内。 - Copy the string into a four-byte buffer (using one of the methods I suggested) and then reverse it by swapping byte
0
with byte3
, and byte1
with byte2
. - The same, but do the reversing using a standard "endianness-swapping" algorithm like the one on this page.
- Iterate through first (and only) four characters using
characterAtIndex:
, and insert them into a four-byte buffer in reverse. Remember thatcharacterAtIndex:
returns a UTF-16 character, but this can be easily cast to as ASCII character assuming it is within the ASCII range.
这篇关于如何将NSString转换为char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!