2016-10-08 45 views
0

我有一個字符串「ZB3NNXAMNB/x6JpAryCd0g ==」,它在java中解碼並存儲在byte []中。但我不確定基於哪種字符集在Java中進行解碼。在objc中解碼字符串,如在Java中

public byte[] decode(String src) { 
     return decode(src.getBytes(StandardCharsets.ISO_8859_1)); 
    } 

當我檢查了Java中解碼方法的方法簽名,如上所示,默認顯示的字符集是ISO_8859_1。這是否意味着通過以下代碼進行解碼所使用的字符集是ISO_8859_1?

salt = Base64.getDecoder().decode(values.getProperty("s")); 

我想要做的ObjectiveC同樣的事情,這是我用下面的代碼

NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64SaltString options:0]; 
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSISOLatin1StringEncoding]; 

沒有,但我不知道,上面的代碼是正確的。此外,不同之處在於Java代碼將已解碼的數據存儲到byte []中,並在objectiveC中作爲String返回。所以我無法比較解碼數據並確保。任何人都可以建議我比較解碼數據的方法嗎?就像將解碼後的數據轉換爲單一格式,iOS和Java一樣。期待尋求幫助。

回答

1

java代碼沒有顯示編碼。 你必須知道哪些編碼Java SDK的使用:

System.out.println("Default Charset=" + Charset.defaultCharset()); 

如果您的Java SDK的UTF-8上運行,代碼:src.getBytes(StandardCharsets.ISO_8859_1)可以UTF-8編碼字符串更改爲ISO_8859_1。

你可以看到在Objective-C代碼:

- (void)testEncoding { 
    NSString *originStr = @"ñèÒ"; 

    //Encode : 
    NSData *utf8Data = [originStr dataUsingEncoding:NSUTF8StringEncoding]; 
    NSData *latinData = [originStr dataUsingEncoding:NSISOLatin1StringEncoding]; 

    NSLog(@"1.0: change a string's encoding ==>\n%@ ,%@",utf8Data,latinData); 

    NSString *utf8Base64String = [utf8Data base64EncodedStringWithOptions:0]; 
    NSString *latinDataBase64String = [latinData base64EncodedStringWithOptions:0]; 

    NSLog(@"1.1 encoding the data(byte[]) to base64string ==>\n%@ ,%@",utf8Base64String,latinDataBase64String); 

    //Decode : 
    NSData *utf8DecodeData = [[NSData alloc] initWithBase64EncodedString:utf8Base64String options:0]; 
    NSData *latinDecodeData = [[NSData alloc] initWithBase64EncodedString:latinDataBase64String options:0]; 

    NSLog(@"2.0 decode the base64string to data(byte[])==>\n %@ ,%@",utf8DecodeData,latinDecodeData); 

    NSString *utf8DecodeString = [[NSString alloc] initWithData:utf8DecodeData encoding:NSUTF8StringEncoding]; 
    NSString *latinDecodeString = [[NSString alloc] initWithData:latinDecodeData encoding:NSISOLatin1StringEncoding]; 

    NSLog(@"2.1 change the data(byte[]) the string with it's encoding ==>\n %@ ,%@",utf8DecodeString,latinDecodeString); 
} 

結果是

1.0: change a string's encoding ==> 
<c3b1c3a8 c29ac392> ,<f1e89ad2> 
1.1 encoding the data(byte[]) to base64string ==> 
w7HDqMKaw5I= ,8eia0g== 
2.0 decode the base64string to data(byte[])==> 
<c3b1c3a8 c29ac392> ,<f1e89ad2> 
2.1 change the data(byte[]) the string with it's encoding ==> 
ñèÒ ,ñèÒ 

當你改變字符串@"ñèÒ"默認情況下是UTF-8編碼NSISOLatin1StringEncoding,你會得到一個totoal不同的數據<f1e89ad2>和base64string 8eia0g==

對於你的情況,你的jav當將字符串更改爲數據(字節[])取決於您的Java環境時,代碼src.getBytes(StandardCharsets.ISO_8859_1)將得到不同的值。所以,我做一個測試解碼你的base64string,但我不知道它的編碼是當期的:

NSString *str = @"ZB3NNxAMNB/x6JpAryCd0g=="; 
NSData *data = [[NSData alloc] initWithBase64EncodedString:str options:0]; 

NSString *resultString = @""; 
for(NSInteger i=1;i<16;i++) { 
    NSString *strDe = [[NSString alloc] initWithData:data encoding:i]; 
    resultString = [NSString stringWithFormat:@"%@%zd => %@\n",resultString,i,strDe]; 
} 

NSLog(@"%@",resultString); 

結果是:

1 => dÍ74ñè@¯ Ò 
2 => d˝74æŁÜ@fl µ¼ 
3 => (null) 
4 => (null) 
5 => dÍ74ñè@¯ Ò 
6 => (null) 
7 => (null) 
8 => dヘ74咫ッ 旆 
9 => dÍ74ńč@Ż Ň 
10 => 搝촷ဌ㐟驀꼠鷒 
11 => dН74сиљ@Ї ќТ 
12 => (null) 
13 => (null) 
14 => (null) 
15 => dÍ74ńčš@Ż ťŇ 
+0

感謝您的信息,它描述得很清楚。那麼,你是說在java字節數組中解碼的數據,index by index是不是可以與objectiveC解碼數據進行比較? –