• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSString encoding problem with extended characters ( AKA : converting NSString to std::string)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSString encoding problem with extended characters ( AKA : converting NSString to std::string)


  • Subject: NSString encoding problem with extended characters ( AKA : converting NSString to std::string)
  • From: Alexander Hartner <email@hidden>
  • Date: Tue, 20 Mar 2007 00:54:10 +0000

I am obtaining a NSString from the iSync API via :

[changeRecord objectForKey:@"first name"]

and I am trying to convert this string into a std::string. I tried using :

std::string * firstName = new std::string([[changeRecord objectForKey:@"first name"] UTF8String]);

which seems to work in most cases, however sometime the string contains characters from the extended character set (>127). I managed to obtain on of these strings and encode is using encoding:NSNonLossyASCIIStringEncoding and ended up with Bj\366rn instead of Björn. After some poking around and trying out many of the other encoding I ended up with the examples at the bottom of this message.

I then put together a test program (below) which ended up showing that the string does not support any of the available encoding. This leads me to questions how can I convert the String obtained from the iSync API to a std::string in the correct encoding. I checked the address book and made sure that Björn was entered using the same keyboard combination as the commented line below, to ensure that it's the same character obtained from the literal string and the iSync API. For some reason it didn't work.

NSString * bjorn = [NSString stringWithCString:"Bj\366rn" encoding:NSNonLossyASCIIStringEncoding];
//NSString * bjorn = "Björn";
const NSStringEncoding* encoding = [NSString availableStringEncodings];

while (*encoding)
{
if ([bjorn canBeConvertedToEncoding:(*encoding)])
{
std::string * value = new std::string([bjorn cStringUsingEncoding: (*encoding)]);
NSLog(@"Encoding : %@",[NSString localizedNameOfStringEncoding:*encoding]);
cout << "Encoding : " << "String : " << *value << endl;
const char * chars = [bjorn cStringUsingEncoding:(*encoding)];
while (*chars)
{
cout << *chars << endl;
chars++;
}
}
else
{
NSLog(@"Encoding : %@ NOT SUPPORTED",[NSString localizedNameOfStringEncoding:*encoding]);
}
encoding++;
}



This might provide some further information :

NSString * one = [changeRecord objectForKey:@"first name"];
unsigned int indexTemp;
for (indexTemp=0;indexTemp<[one length];indexTemp++)
{
	NSLog(@"%i : %C",indexTemp,[one characterAtIndex:indexTemp]);
}

Produces :
0 : B
1 : j
2 : ö
3 : r
4 : n

This :
{
	const char * chars = [one cStringUsingEncoding:NSUTF8StringEncoding];
	std::string * charsOne = new std::string(chars);
	int index = 0;
	while (chars[index] != nil)
	{
		cout << index << " : " << *charsOne << " : " << chars[index] << endl;
		index ++;
	}
}

Produces :
0 : Bj\366rn : B
1 : Bj\366rn : j
2 : Bj\366rn : \
3 : Bj\366rn : 3
4 : Bj\366rn : 6
5 : Bj\366rn : 6
6 : Bj\366rn : r
7 : Bj\366rn : n

and this:
NSString * two = @"Björn";
{
	const char * chars = [two UTF8String];
	int index = 0;
	while (chars[index] != nil)
	{
		cout << index << " : " << chars[index] << endl;
		index ++;
	}
}

produces this :
0 : B
1 : j
2 : \342
3 : \210
4 : \232
5 : \342
6 : \210
7 : \202
8 : r
9 : n



Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: NSString encoding problem with extended characters ( AKA : converting NSString to std::string)
      • From: "Clark Cox" <email@hidden>
    • Re: NSString encoding problem with extended characters ( AKA : converting NSString to std::string)
      • From: Ricky Sharp <email@hidden>
  • Prev by Date: Image manipulation on 10.3
  • Next by Date: Re: NSString encoding problem with extended characters ( AKA : converting NSString to std::string)
  • Previous by thread: Re: Image manipulation on 10.3
  • Next by thread: Re: NSString encoding problem with extended characters ( AKA : converting NSString to std::string)
  • Index(es):
    • Date
    • Thread