String Encodings - NSShiftJISStringEncoding
The following constants are provided by
NSString
as possible string encodings.enum {
NSASCIIStringEncoding = 1,
NSNEXTSTEPStringEncoding = 2,
NSJapaneseEUCStringEncoding = 3,
NSUTF8StringEncoding = 4,
NSISOLatin1StringEncoding = 5,
NSSymbolStringEncoding = 6,
NSNonLossyASCIIStringEncoding = 7,
NSShiftJISStringEncoding = 8,
NSISOLatin2StringEncoding = 9,
NSUnicodeStringEncoding = 10,
NSWindowsCP1251StringEncoding = 11,
NSWindowsCP1252StringEncoding = 12,
NSWindowsCP1253StringEncoding = 13,
NSWindowsCP1254StringEncoding = 14,
NSWindowsCP1250StringEncoding = 15,
NSISO2022JPStringEncoding = 21,
NSMacOSRomanStringEncoding = 30,
NSUTF16StringEncoding = NSUnicodeStringEncoding,
NSUTF16BigEndianStringEncoding = 0x90000100,
NSUTF16LittleEndianStringEncoding = 0x94000100,
NSUTF32StringEncoding = 0x8c000100,
NSUTF32BigEndianStringEncoding = 0x98000100,
NSUTF32LittleEndianStringEncoding = 0x9c000100,
NSProprietaryStringEncoding = 65536
};
Constants
NSShiftJISStringEncoding
- 8-bit Shift-JIS encoding for Japanese text.
- Discussion of [NSString NSShiftJISStringEncoding]
These values represent the various character encodings supported by the
NSString
classes. This is an incomplete list. Additional encodings are defined in String Programming Guide for Core Foundation (see CFStringEncodingExt.h
); these encodings can be used with NSString
by first passing the Core Foundation encoding to the CFStringConvertEncodingToNSStringEncoding
function.Example of [NSString NSShiftJISStringEncoding]
NSString* barcode = symbol.data;
if ([barcode canBeConvertedToEncoding:NSShiftJISStringEncoding]) {
barcode = [NSString stringWithCString:[barcode cStringUsingEncoding: NSShiftJISStringEncoding] encoding:NSUTF8StringEncoding];
}
Example of [NSString NSShiftJISStringEncoding]
- (void)dataArrived:(NSData*)data
{
[_mutableData appendData:data];
NSUInteger length = [_mutableData length];
NSString *string = [[[NSString alloc] initWithData:_mutableData
encoding:NSShiftJISStringEncoding] autorelease];
if ( ! string && length )
{
// try without the last byte
--length;
string = [[[NSString alloc] initWithBytes:data.bytes
length:length
encoding:NSShiftJISStringEncoding] autorelease];
}
if ( ! string && length )
{
// we have a problem: the data are invalid
return;
}
// remove processed bytes
[_mutableData replaceBytesInRange:NSMakeRange(0, length) withBytes:NULL length:0];
// now, we can append string
}
Example of [NSString NSShiftJISStringEncoding]
+ (NSString *)encodedStringWithContentsOfURL:(NSURL *)url
{
// Get the web page HTML
NSData *data = [NSData dataWithContentsOfURL:url];
// response
int enc_arr[] = {
NSUTF8StringEncoding, // UTF-8
NSShiftJISStringEncoding, // Shift_JIS
NSJapaneseEUCStringEncoding, // EUC-JP
NSISO2022JPStringEncoding, // JIS
NSUnicodeStringEncoding, // Unicode
NSASCIIStringEncoding // ASCII
};
NSString *data_str = nil;
int max = sizeof(enc_arr) / sizeof(enc_arr[0]);
for (int i=0; i<max; i++) {
data_str = [
[NSString alloc]
initWithData : data
encoding : enc_arr[i]
];
if (data_str!=nil) {
break;
}
}
return data_str;
}