2012-05-29 185 views
0

我想使用波蘭民族字符Base64編碼。例如:Base64和utf8 /國家字符編碼

"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[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/"; 

    //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 

女巫回到我身邊(bylinlin Ëdecoder):

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

我打電話這種方式:

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

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

encodeString = [self Base64Encode:data]; 

回答

1

在線譯碼器是ISO-8859-1頁面,而不是UTF-8。如果你強制使用UTF-8,它就會起作用。

此外,編碼版本的差異可能是由於組合與分解字符(不確定)。

+0

現在我再次看到,這可能是什麼區別。 :) – Wevah