繁体   English   中英

Base64和utf8 /国家字符编码

[英]Base64 and utf8 / National characters encoding

我想对Base64编码使用波兰语国家字符。 例如:

"zażółć gęślą jaźń"

应该:

emEmIzM4MDvzJiMzMjI7JiMyNjM7IGcmIzI4MTsmIzM0NztsJiMyNjE7IGphJiMzNzg7JiMzMjQ7

但实施此解决方案后:

-(NSString *)Base64Encode:(NSData *)data{
    //Point to start of the data and set buffer sizes
    int inLength = [data length];
    int outLength = ((((inLength * 4)/3)/4)*4) + (((inLength * 4)/3)%4 ? 4 : 0);
    const char *inputBuffer = [data bytes];
    char *outputBuffer = malloc(outLength);
    outputBuffer[outLength] = 0;

    //64 digit code
    static char Encode[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    //start the count
    int cycle = 0;
    int inpos = 0;
    int outpos = 0;
    char temp;

    //Pad the last to bytes, the outbuffer must always be a multiple of 4
    outputBuffer[outLength-1] = '=';
    outputBuffer[outLength-2] = '=';


    while (inpos < inLength){
        switch (cycle) {
            case 0:
                outputBuffer[outpos++] = Encode[(inputBuffer[inpos]&0xFC)>>2];
                cycle = 1;
                break;
            case 1:
                temp = (inputBuffer[inpos++]&0x03)<<4;
                outputBuffer[outpos] = Encode[temp];
                cycle = 2;
                break;
            case 2:
                outputBuffer[outpos++] = Encode[temp|(inputBuffer[inpos]&0xF0)>> 4];
                temp = (inputBuffer[inpos++]&0x0F)<<2;
                outputBuffer[outpos] = Encode[temp];
                cycle = 3;                  
                break;
            case 3:
                outputBuffer[outpos++] = Encode[temp|(inputBuffer[inpos]&0xC0)>>6];
                cycle = 4;
                break;
            case 4:
                outputBuffer[outpos++] = Encode[inputBuffer[inpos++]&0x3f];
                cycle = 0;
                break;                          
            default:
                cycle = 0;
                break;
        }
    }
    NSString *pictemp = [NSString stringWithUTF8String:outputBuffer];
    free(outputBuffer); 
    return pictemp;
}

当然,我得到一些不同的东西:

emHFvMOzxYLEhyBnxJnFm2zEhSBqYcW6xYQ

女巫回到我身边(通过在线解码器 ):

zażółć gęślą jaźń

我这样称呼它:

NSString* str= _@"zażółć gęślą jaźń";
NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding];

NSString * encodeString = [[[NSString alloc] init] autorelease];

encodeString = [self Base64Encode:data];

在线解码器位于ISO-8859-1页面上,而不是UTF-8。 如果您强制使用UTF-8,它将起作用。

同样,编码版本的差异可能是由于组合字符和分解字符(不确定)所致。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM