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;
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.
I am using GTResourceFork, a Cocoa wrapper for accessing resource forks. The method I am calling is:
– (NSArray *)usedResourcesOfType: (ResType)type;
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'
Thanks in advance,
Lance
The problem with
getCharacters:range:is that it gives you UTF-16 characters (unichars), whereas you want ASCII.*(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 aResTypevalue. Whether this is efficient enough depends on how often you want to do these conversions.Another option is to use
getBytes:maxLength:usedLength:encoding:options:range:remainingRange:with the encoding set toNSASCIIStringEncodingorNSUTF8StringEncoding, the destination buffer set to a pointer to an existingResTypevariable, and the maximum length set to4(orsizeof (ResType)).Update:
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:
The output is
' FTR' = 1381254688. So, if you want to convert fromNSStrings to these values, here are a few options:0with byte3, and byte1with byte2.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.