[英]How to convert a NSString to UTf8string for using multiple languages?
[英]How to convert a NSString to UTF in the quoted printable form using iOS SDK?
我需要將一個字符串(可能帶有非拉丁字符)轉換為 UTF-8,進一步編碼為帶引號的可打印形式。
例如,我有一個字符串,例如“привет”,我需要將其轉換為“=D0=BF=D1=80.......”之類的東西。
有人可以幫助我嗎?
上面的答案確實有效,但它們對整個字符串進行了編碼。 這通常不是您想要的,特別是如果您將此代碼用於編寫 email 之類的事情。
您實際上可能追求的是一種僅在 QP 中對那些非 ascii 字符進行編碼的方法。
輸入:
Simply place the bass—that's the whole bass—into the Bass-o-matic.
Output(天真):
=53=69=6D=70=6C=79=20=70=6C=61=63=65=20=74=68=65=20=62=61=73=73=E2=80=94=74=68=61=74=E2=80=99=73=20=74=68=65=20=77=68=6F=6C=65=20=62=61=73=73=E2=80=94=69=6E=74=6F=20=74=68=65=20=42=61=73=73=2D=6F=2D=6D=61=74=69=63=2E
Output(改進):
Simply place the bass=E2=80=94that=E2=80=99s the whole bass=E2=80=94into the Bass-o-matic.
更像這樣的東西:
- (NSString *)qpEncodedStringWithString:(NSString *)string {
NSCharacterSet *asciiSet = [NSCharacterSet characterSetWithRange:NSMakeRange(0, 127)];
NSCharacterSet *invertedAscii = [asciiSet invertedSet];
// if the string contains non-ascii characters, we need to encode them
// otherwise, we'll just return the string below
if ([string rangeOfCharacterFromSet:invertedAscii].location != NSNotFound) {
// because the % sign is, itself, an ascii character, we must first replace it
// with a placeholder unlikely to occur in a string, but still within ascii
NSString *percentPlaceholder = @"QqQxXxPpP";
string = [string stringByReplacingOccurrencesOfString:@"%" withString:percentPlaceholder];
// use Apple's method to percent encode the string
string = [string stringByAddingPercentEncodingWithAllowedCharacters:asciiSet];
// replace those percents with = signs
string = [string stringByReplacingOccurrencesOfString:@"%" withString:@"="];
// and restore the true percent symbols
string = [string stringByReplacingOccurrencesOfString:percentPlaceholder withString:@"%"];
}
return string;
}
好吧,我不會遵循這種編碼的所有規范。 假設你沒有拖尾空格、換行符和其他不好的東西,這里是你需要的代碼:
@interface NSString(QuotedPrintable)
- (NSString *)quotedPrintable;
@end
@implementation NSString(QuotedPrintable)
- (NSString *)quotedPrintable {
const char *utfString = [self UTF8String];
char *p = utfString;
NSMutableString *result = [NSMutableString stringWithString:@""];
while (*p) {
char chars[2];
char c = *p;
for (int i = 0; i < 2; i++) {
int val = ((int)c & 15);
if (val < 10) {
chars[i] = '0'+val;
} else {
chars[i] = 'A'+(val-10);
}
c = c >> 4;
}
[result appendFormat:@"=%c%c", chars[1], chars[0]];
p++;
}
return [NSString stringWithString:result];
}
@end
對於привет它給出=D0=BF=D1=80=D0=B8=D0=B2=D0=B5=D1=82
這與安東的回答幾乎相同,只是更短:
- (NSString *)quotedPrintableString
NSMutableString *encoded = [NSMutableString stringWithCapacity:3*[self length]];
const char *characters = [self UTF8String];
NSUInteger length = strlen(characters);
for (NSUInteger i = 0; i < length; ++i) {
char character = characters[i];
int left = character & 0xF;
int right = (character >> 4) & 0xF;
[encoded appendFormat:@"=%X%X", right, left];
}
return encoded;
}
用這個 -
NSString *yourString = @"Hello world";
const char *utfString = [yourString UTF8String];
NSSTring* finalEncodedString = [NSString stringWithUTF8String:utfString];
現在打印“finalEncodedString”,它現在是 UTF8 編碼的字符串。
試試[NSString stringWithUTF8String:yourString];
NSString *yourString = @"Hello world";
char *utfString = [yourString UTF8String];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.