Hello All,

I am attempting to use UnicodeString as a basis for unicode functionality in my C++ libraries, that are in turn going to be used in multiple environments - one of which will be Mac/Cocoa.

One of the aims in my C++ interfaces is to explicitly state when the user has to provide me with Unicode compatible data, and when not - via the use of the ICU UnicodeString object.

I've written a simple test program to see if the Unicode data pumped into a UnicodeString object, can in turn then be used by the NSString Cocoa API.  So far, I'm having some, what I believe to be, simple issues, that are stopping me dead.  I'm assuming that I've missed something fundamental - either the way in which I'm populating the UnicodeString, or passing it through to the NSString objects is wrong... 

Always good, are code examples... what follows is:

- populate UnicodeString with data (via UNICODE_STRING_SIMPLE), in this case, the word 'breakfast' in German 
- get a (const char *) ptr to that data, and feed it into NSString - telling NSString what format this is (UTF-16 in this case)
- print out the contents of the NSString.

If everything goes well, then the NSString contents will be the same as what I fed the UnicodeString - right?

Not in my case, it all goes bad.  I would really appreciate it if someone could cast their eyes over the code below, and perhaps spread some light on the issue.



-- the output produced by the code below --

[Session started at 2006-09-13 15:24:30 +0200.]
2006-09-13 15:24:30.921 UnicodeCPPSpike[2322] UnicodeWord length is 11
2006-09-13 15:24:30.934 UnicodeCPPSpike[2322] the word is: , and 0 in length
2006-09-13 15:24:30.934 UnicodeCPPSpike[2322] the utf8 string, printed simply is:
2006-09-13 15:24:30.934 UnicodeCPPSpike[2322] utf8breakfastLength = 0
2006-09-13 15:24:30.934 UnicodeCPPSpike[2322] converted from cpp's utf8 format, using stringWithUTF8String:

UnicodeCPPSpike has exited with status 0.

-- end of the output --



---- the code ----
int main (int argc, const char * argv[]) {


NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];


// and the UNICODE_STRING_SIMPLE() will nicely convert my literals into UnicodeString objects, then I've
// got the ICU internal format - i.e. UTF-16.  Now should be an easy matter of providing this to the NSString 
UnicodeString word( UNICODE_STRING_SIMPLE("Frühstück") );


NSLog(@"UnicodeWord length is %d", word.length());

// the NSString stringWithCString:encoding: doesn't have a Cocoa enum for the
// UTF-16 encoding, but can make one via CFStringConvertEncodingToNSStringEncoding() method
UInt32 encodingUTF16 = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF16);

// create an NSString with this 'word' object, telling NSString what format the
// unicode chars are using... in this case, the UnicodeString uses UTF-16 internally.
NSString *breakfast = [NSString stringWithCString:(const char *)word.getTerminatedBuffer() 
encoding:encodingUTF16];
int breakfastLength = [breakfast length];
NSLog(@"the word is: %@, and %d in length", breakfast, breakfastLength);


// Take the NSString data as UTF-8 and put it into a standard, boring old std::string 
const char *utf8breakfast = [breakfast UTF8String];
int utf8breakfastLength = strlen(utf8breakfast);
NSLog(@"the utf8 string, printed simply is: %s", utf8breakfast);
NSLog(@"utf8breakfastLength = %d", utf8breakfastLength);


// hmmm... things are not going well, print this byte by byte...
for(int index = 0; utf8breakfast[index]; ++index)
std::cout << "char " << index << ": " << utf8breakfast[index] << std::endl;


// convert the utf8 back into NSString, we should see 'Frühstück' 
NSString *convertedFromCPP = [NSString stringWithUTF8String:utf8breakfast];
NSLog(@"converted from cpp's utf8 format, using stringWithUTF8String: %@", convertedFromCPP);


[pool release];


    return 0;
}

---- the end of the code ----
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Sisuite-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sisuite-users

Reply via email to