Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1115047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:05:56+00:00 2026-05-17T03:05:56+00:00

I am trying to convert an NSString to a ResType , as defined below

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T03:05:57+00:00Added an answer on May 17, 2026 at 3:05 am

    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 a ResType value. 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 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)).


    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:

    #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);
    }
    

    The output is ' FTR' = 1381254688. So, if you want to convert from NSStrings to these values, here are a few options:

    • Copy the string into a four-byte buffer (using one of the methods I suggested) and then reverse it by swapping byte 0 with byte 3, and byte 1 with byte 2.
    • 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 that characterAtIndex: returns a UTF-16 character, but this can be easily cast to as ASCII character assuming it is within the ASCII range.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert old QuickTime framework code to the 64-bit Cocoa-based QTKit on
I'm trying to convert the below SQL query to HQL and am having a
Hey, I am trying to convert NSString to a C string using cStringUsingEncoding but
I'm trying to convert an NSString to a single char. When I write [[NSLocale]
I'm trying to convert a NSString to an NSDate. If the iPhone region is
I'm trying to convert the following NSString api call to a NSURL object: http://beta.com/api/token=
I'm trying to convert an NSString object to an NSNumber with the same numerical
I am trying to convert a standard std::string into an NSString but I'm not
I am trying to figure out how to take a given NSString and convert
I'm trying to figure out how to take the individual characters in an NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.