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

时间:2021-11-16 20:13:55

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

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

4 个解决方案

#1


The no-arg getBytes() method uses the platform default encoding, which is probably not UTF-16LE. Try getBytes("UTF-16LE").

no-arg getBytes()方法使用平台默认编码,该编码可能不是UTF-16LE。试试getBytes(“UTF-16LE”)。

#2


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

#3


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

#4


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]

#1


The no-arg getBytes() method uses the platform default encoding, which is probably not UTF-16LE. Try getBytes("UTF-16LE").

no-arg getBytes()方法使用平台默认编码,该编码可能不是UTF-16LE。试试getBytes(“UTF-16LE”)。

#2


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

#3


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

#4


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]