Java在UTF-16上编码Charset

发布于 2025-02-03 17:57:03 字数 145 浏览 2 评论 0 原文

您能帮助我吗,我可以在UTF-16“测试”输出0074 0065 0073 0074上对Java中的Charset进行编码,对此有一些功能吗?

String x = "test";
System.out.println(x);

can you help my, can i encode charset in java on UTF-16 "test" output 0074 0065 0073 0074, are there some function for this?

String x = "test";
System.out.println(x);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷鸟归林 2025-02-10 17:57:03

Java的标准方法将是方法 getBytes(charset Charset) class string 的。为了证明我刚刚写了一个小方法:

private static void encodingTest() {
    String testStr = "test";
    System.out.println(StringUnicodeEncoderDecoder.encodeStringToUnicodeSequence(testStr));
    StringBuilder sb = new StringBuilder();
    byte[] bytes = testStr.getBytes(StandardCharsets.UTF_16);
    for(byte b: bytes) {
        sb.append(b).append(" ");
    }
    System.out.println(sb.toString());
}

该方法的输出是:

\u0074\u0065\u0073\u0074
-2 -1 0 116 0 101 0 115 0 116 

请注意,值116、101、115、116是十进制值,如果转换为十六进制,则为74、65、73和74-这就是您正在寻找。类 StringunicodeCoderDecoder 您在我的代码中看到的,这为您提供了输出 \ u0074 \ u0065 \ u0073 \ u0073 \ u0074 不是标准Java的一部分。它是我写的开源Mgntutils库的一部分。但是在这种情况下,这对您可能非常有用。这是在这里 a href =“ https://github.com/michaelgantman/mgnt/releases” rel =“ nofollow noreferrer”> github 作为JAR(包括源代码和Javadoc),

这是一个修改的代码

private static void encodingTest() {
    String testStr = "test";
    String encoded = StringUnicodeEncoderDecoder.encodeStringToUnicodeSequence(testStr);
    System.out.println(encoded);
    System.out.println(encoded.replaceAll("\\\\u", " "));
    System.out.println(encoded.replaceAll("\\\\u", ""));
}

:是:

\u0074\u0065\u0073\u0074
 0074 0065 0073 0074
0074006500730074

The standard method of Java would be method getBytes(Charset charset) of class String. To demonstrate I just wrote a small method:

private static void encodingTest() {
    String testStr = "test";
    System.out.println(StringUnicodeEncoderDecoder.encodeStringToUnicodeSequence(testStr));
    StringBuilder sb = new StringBuilder();
    byte[] bytes = testStr.getBytes(StandardCharsets.UTF_16);
    for(byte b: bytes) {
        sb.append(b).append(" ");
    }
    System.out.println(sb.toString());
}

And the output of that method is:

\u0074\u0065\u0073\u0074
-2 -1 0 116 0 101 0 115 0 116 

Note that values 116, 101, 115, 116 are decimal values which if converted to Hex would be 74, 65, 73, and 74 - which is what you are looking for. The class StringUnicodeEncoderDecoder that you see in my code and that gives you the output \u0074\u0065\u0073\u0074 is not part of a standard Java. It is part of an Open Source MgntUtils library written by me. But it could be very useful to you in this case. Here is the Javadoc for the class StringUnicodeEncoderDecoder. The library itself could be obtained as Maven artifacts from here or from Github as a jar (including source code and Javadoc)

Here is a modified code:

private static void encodingTest() {
    String testStr = "test";
    String encoded = StringUnicodeEncoderDecoder.encodeStringToUnicodeSequence(testStr);
    System.out.println(encoded);
    System.out.println(encoded.replaceAll("\\\\u", " "));
    System.out.println(encoded.replaceAll("\\\\u", ""));
}

And the output would be:

\u0074\u0065\u0073\u0074
 0074 0065 0073 0074
0074006500730074
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文