如何将字符串转换为UTF-16

时间:2015-05-03 17:54:45

标签: java utf-16

如何在java中将字符串转换为UTF-16?

我正在将下面的objc代码转换为java,但是java代码并没有给我这个代码相同的结果

   NSData *inputData = [pm dataUsingEncoding:NSUTF16LittleEndianStringEncoding];
    NSString *encodedString = [inputData base64EncodedString];
    pm = [IFunctions replaceString:encodedString replaceChar:@"=" replaceWithChar:@"-"];

Java代码

String s_decoded = new String(pm.getBytes(), "UTF-16LE");
pm = Base64.encode(s_decoded.getBytes()).toString().replace("=", "-");

1 个答案:

答案 0 :(得分:1)

byte[]   bytesEncoded = Base64.encodeBase64(str.getBytes("UTF-16LE"));
String stringEncoded = new String(bytesEncoded);