简体   繁体   English

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

[英]how to convert a string to UTF-16

how to convert a string to UTF-16 in java ? 如何在Java中将字符串转换为UTF-16?

i am converting the below objc code to java , but the java code does not give me same result as this code 我正在将下面的objc代码转换为java,但是java代码没有给我与该代码相同的结果

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

Java code Java代码

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

The no-arg getBytes() method uses the platform default encoding, which is probably not UTF-16LE. no-arg getBytes()方法使用平台默认编码,可能不是UTF-16LE。 Try getBytes("UTF-16LE") . 尝试getBytes("UTF-16LE")

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

Refer to this code and modify yours. 请参考此代码并修改您的代码。

public class UseTheForce {
        public static void main(final String[] args)
            throws java.io.UnsupportedEncodingException {
            for (final byte b : args[0].getBytes(args[1])) {
                System.out.printf("%1$02X ", (b & 0xFF));
            }
            System.out.println();
        }
    }
    Test

    $ java UseTheForce luke US-ASCII
    6C 75 6B 65

    $ java UseTheForce luke UTF-8
    6C 75 6B 65

    $ java UseTheForce luke UTF-16
    FE FF 00 6C 00 75 00 6B 00 65

    $ java UseTheForce luke UTF-16BE
    00 6C 00 75 00 6B 00 65

    $ java UseTheForce luke UTF-16LE
    6C 00 75 00 6B 00 65 00

    $ java UseTheForce luke UTF-32
    00 00 00 6C 00 00 00 75 00 00 00 6B 00 00 00 65

You need to use getBytes() method and specify desired charset: 您需要使用getBytes()方法并指定所需的字符集:

public static void main(String[] args) throws UnsupportedEncodingException {
    char ch;
    ch = 0x0001;
    System.out.println(Arrays.toString((String.valueOf(ch)).getBytes(StandardCharsets.UTF_16LE)));
    ch = 0x0111;
    System.out.println(Arrays.toString((String.valueOf(ch)).getBytes(StandardCharsets.UTF_16LE)));
    ch = 0x1111;
    System.out.println(Arrays.toString((String.valueOf(ch)).getBytes(StandardCharsets.UTF_16LE)));
}

Output: 输出:

[1, 0]
[17, 1]
[17, 17]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM