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ńčš@Ż ťŇ
感謝您的信息,它描述得很清楚。那麼,你是說在java字節數組中解碼的數據,index by index是不是可以與objectiveC解碼數據進行比較? –