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
,您将获得一个完全不同的数据<f1e89ad2>
和 base64string8eia0g==
对于您的情况,src.getBytes(StandardCharsets.ISO_8859_1)
将字符串更改为 data(byte[]) 时,您的 java 代码将获得不同的值,具体取决于您的 java 环境。所以我做了一个测试来解码你的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ńčš@Ż ťŇ