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 6564375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:57:32+00:00 2026-05-25T13:57:32+00:00

I have an NSString with a value of Jose (an accent on the e).

  • 0

I have an NSString with a value of Jose (an accent on the e). I try to convert it to a C string as follows:

char str [[myAccentStr length] + 1];
[myAccentStr getCString:str maxLength:[myAccentStr length] + 1 encoding:NSUTF32StringEncoding];

but str ends up being an empty string. What gives? I tried UTF8 and UTF16 too. It gets passed to another function later on and when that funcsion calls lstrlen on it, the size comes out as zero.

  • 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-25T13:57:32+00:00Added an answer on May 25, 2026 at 1:57 pm

    The docs for NSString getCString:maxLength:encoding says:

    You can use canBeConvertedToEncoding: to check whether a string can be
    losslessly converted to encoding. If it can’t, you can use
    dataUsingEncoding:allowLossyConversion: to get a C-string
    representation using encoding, allowing some loss of information (note
    that the data returned by dataUsingEncoding:allowLossyConversion: is
    not a strict C-string since it does not have a NULL terminator).

    Using the NSString method dataUsingEncoding:allowLossyConversion: does the trick. Here’s a code example:

    NSString *myAccentStr = @"José";
    char str[[myAccentStr length] + 1];
    
    // NSString * to C String (char*)
    NSData *strData = [myAccentStr dataUsingEncoding:NSMacOSRomanStringEncoding 
                                    allowLossyConversion:YES];
    memcpy(str, [strData bytes], [strData length] + 1);
    str[[myAccentStr length]] = '\0';
    NSLog(@"str (from NSString* to c string): %s", str);
    
    // C String (char*) to NSString *   
    NSString *newAccentStr = [NSString stringWithCString:str 
                                                encoding:NSMacOSRomanStringEncoding];
    NSLog(@"newAccentStr (from c string to NSString*):  %@", newAccentStr);
    

    The output from that NSLog is:

    str (from NSString* to c string): José

    newAccentStr (from c string to NSString*): José

    So far I’ve only seen this work properly when using the NSMacOSRomanStringEncoding.


    Edit

    Changing this to a community wiki. Please feel free to edit.

    hooleyhoop had some great points, so I thought I would try to make code that is as verbose as possible. If I’m missing anything, someone please chime in.

    Also – Not sure why [NSString canBeConvertedToEncoding:] is returning YES even though the [NSString getCString:maxLength:encoding:] function definitely isn’t working right (as seen by the output).

    Here’s some code to help in analyzing what works / what doesn’t:

    // Define Block variable to tests out different encodings
    void (^tryGetCStringUsingEncoding)(NSString*, NSStringEncoding) = ^(NSString* originalNSString, NSStringEncoding encoding) {
        NSLog(@"Trying to convert \"%@\" using encoding: 0x%X", originalNSString, encoding);
        BOOL canEncode = [originalNSString canBeConvertedToEncoding:encoding];
        if (!canEncode)
        {
            NSLog(@"    Can not encode \"%@\" using encoding %X", originalNSString, encoding);
        }
        else
        {
            // Try encoding using NSString getCString:maxLength:encoding:
            NSUInteger cStrLength = [originalNSString lengthOfBytesUsingEncoding:encoding];
            char cstr[cStrLength];
            [originalNSString getCString:cstr maxLength:cStrLength encoding:encoding];
            NSLog(@"    Converted(1): \"%s\"  (expected length: %u)",
                  cstr, cStrLength);
    
            // Try encoding using NSString dataUsingEncoding:allowLossyConversion:          
            NSData *strData = [originalNSString dataUsingEncoding:encoding allowLossyConversion:YES];
            char cstr2[[strData length] + 1];
            memcpy(cstr2, [strData bytes], [strData length] + 1);
            cstr2[[strData length]] = '\0';
            NSLog(@"    Converted(2): \"%s\"  (expected length: %u)",
                  cstr2, [strData length]);
        }
    };
    
    NSString *myAccentStr = @"José";
    
    // Try out whatever encoding you want
    tryGetCStringUsingEncoding(myAccentStr, NSUTF8StringEncoding);
    tryGetCStringUsingEncoding(myAccentStr, NSUTF16StringEncoding);
    tryGetCStringUsingEncoding(myAccentStr, NSUTF32StringEncoding);
    tryGetCStringUsingEncoding(myAccentStr, NSMacOSRomanStringEncoding);
    

    Results:

    > Trying to convert "José" using encoding: 0x4
    >     Converted(1): ""  (expected length: 5)
    >     Converted(2): "José"  (expected length: 5)
    > Trying to convert "José" using encoding: 0xA
    >     Converted(1): ""  (expected length: 8)
    >     Converted(2): "ˇ˛J"  (expected length: 10)
    > Trying to convert "José" using encoding: 0x8C000100
    >     Converted(1): ""  (expected length: 16)
    >     Converted(2): "ˇ˛"  (expected length: 20)
    > Trying to convert "José" using encoding: 0x1E
    >     Converted(1): "-"  (expected length: 4)
    >     Converted(2): "José"  (expected length: 4)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string value. NSString *myString = @Latitude:-31.9504140#Longitude:115.8606040; How can I retrieve the
I have an NSString named 'you' with value This is a you string!. I
I have an object which contains the NSString value. NSString *string =@50 ,50; i
i have an NSString that is value in plist format. i download this string
I have: NSString *promise = @thereAreOtherWorldsThanThese; which I'm trying to transform into the string:
I have an NSString *string=@606 and I want to add a : colon after
Have mercy, newbie here. I have a NSString value that I construct on the
i have a NSString instance ,i want to retrieve value from it and store
I have an NSString with the value @1:10:39 What's the fastest / most convenient
I have the following code fragment: NSString* value = @value; NSString* key = @key;

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.