I need to convert a string (probably with non latin characters) to UTF-8, further encoded in a quoted-printable form.
For example I have a string, e.g "привет" and I need to convert it to "=D0=BF=D1=80......." something like that.
Can someb开发者_JS百科ody help me?
This is pretty much the same thing as Anton's answer, just a lot shorter:
- (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;
}
Well, I won't follow all the specs of this encoding. Assuming that you don't have tailing whitespaces, newline character and other bad stuff, here is the code you need:
@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
For привет it gives =D0=BF=D1=80=D0=B8=D0=B2=D0=B5=D1=82
The above answers do work, but they encode the whole string. Which isn't usually what you'd want, particularly if you're using this code for something like composing an email.
What you're actually likely after is a method to encode only those non-ascii characters in QP.
Input:
Simply place the bass—that’s the whole bass—into the Bass-o-matic.
Output (naiive):
=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 (improved):
Simply place the bass=E2=80=94that=E2=80=99s the whole bass=E2=80=94into the Bass-o-matic.
Something more like this:
- (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;
}
(builds off of this answer for decoding QP strings from bensnider)
Try [NSString stringWithUTF8String:yourString];
NSString *yourString = @"Hello world";
char *utfString = [yourString UTF8String];
Use This -
NSString *yourString = @"Hello world";
const char *utfString = [yourString UTF8String];
NSSTring* finalEncodedString = [NSString stringWithUTF8String:utfString];
Now print the 'finalEncodedString', it's UTF8 encoded string now.
精彩评论