|
From: | Fred Kiefer |
Subject: | Re: String encoding question |
Date: | Tue, 25 Nov 2003 01:42:18 +0100 |
User-agent: | Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030821 |
Björn Giesler wrote:
I'm trying to read vcard files (example attached) exported from Apple's Address Book application. I have to use [NSString initWithData:encoding:] on the file contents, because that's what Apple's interface specifies. I've got no idea which encoding these things are in... they look like unicode, because there are two bytes for each character, but when I use NSUnicodeStringEncoding, the result is garbage (when dumped with NSLog()). When I use NSAsciiStringEncoding, NSLog() prints the contents just fine, but the resulting string keeps the extra bytes ([str length] returns 22 for "BEGIN:VCARD"), so I can't parse the file properly. Any ideas? I'm thoroughly lost with this encoding stuff.
Not sure what you did try, but the following code works for me: #include <Foundation/NSAutoreleasePool.h> #include <Foundation/NSData.h> #include <Foundation/NSString.h> int main (int argc, const char *argv[]) { NSData *data; NSString *string; CREATE_AUTORELEASE_POOL(pool); data = [NSData dataWithContentsOfFile: [@"~/GNUstep/HeinzSchenk.vcf" stringByExpandingTildeInPath]]; string = [[NSString alloc] initWithData: data encoding: NSUnicodeStringEncoding]; NSLog(@"Did read: %@", string); RELEASE(string); RELEASE(pool); return 0; }
[Prev in Thread] | Current Thread | [Next in Thread] |