Re: (unicode -> shift-jis) encoding conversion bug?
Re: (unicode -> shift-jis) encoding conversion bug?
- Subject: Re: (unicode -> shift-jis) encoding conversion bug?
- From: Greg Titus <email@hidden>
- Date: Thu, 24 Jan 2002 09:30:50 -0800
On Thursday, January 24, 2002, at 06:22 AM, Jody Fairchild wrote:
a situation in which converting characters from unicode to shift-jis
seems to produce incorrect (or at least counterintuitive) results, e.g:
let's run the following code snippetoid:
NSString *example;
unichar in, out;
NSString *s;
NSData *d;
for (i = 0; i < [length example]; i++)
{
uc = [example characterAtIndex:i];
s = [[NSString alloc] initWithCharacters:&in length:1];
d = [s dataUsingEncoding:NSShiftJISStringEncoding
allowLossyConversion:NO];
[d getBytes:&out];
NSLog(@"unicode = %X, sjis = %X",in,out);
}
I'm betting if you looked at [d length], that you'd find that the length
is 1 in the case of plain 'a'. (I.e. a single byte whose value is 0x61.)
The bug is that you are just assuming that the length of the data is
going to be 2 when you call getBytes:.
Hope this helps,
--Greg