<legend id='CIYDh'><style id='CIYDh'><dir id='CIYDh'><q id='CIYDh'></q></dir></style></legend>
  • <i id='CIYDh'><tr id='CIYDh'><dt id='CIYDh'><q id='CIYDh'><span id='CIYDh'><b id='CIYDh'><form id='CIYDh'><ins id='CIYDh'></ins><ul id='CIYDh'></ul><sub id='CIYDh'></sub></form><legend id='CIYDh'></legend><bdo id='CIYDh'><pre id='CIYDh'><center id='CIYDh'></center></pre></bdo></b><th id='CIYDh'></th></span></q></dt></tr></i><div id='CIYDh'><tfoot id='CIYDh'></tfoot><dl id='CIYDh'><fieldset id='CIYDh'></fieldset></dl></div>

    • <bdo id='CIYDh'></bdo><ul id='CIYDh'></ul>

    <small id='CIYDh'></small><noframes id='CIYDh'>

      <tfoot id='CIYDh'></tfoot>

        将 char 数组转换为字节数组并再次返回

        时间:2023-07-28

          <tfoot id='gtB8O'></tfoot>
          • <bdo id='gtB8O'></bdo><ul id='gtB8O'></ul>

                <small id='gtB8O'></small><noframes id='gtB8O'>

                <i id='gtB8O'><tr id='gtB8O'><dt id='gtB8O'><q id='gtB8O'><span id='gtB8O'><b id='gtB8O'><form id='gtB8O'><ins id='gtB8O'></ins><ul id='gtB8O'></ul><sub id='gtB8O'></sub></form><legend id='gtB8O'></legend><bdo id='gtB8O'><pre id='gtB8O'><center id='gtB8O'></center></pre></bdo></b><th id='gtB8O'></th></span></q></dt></tr></i><div id='gtB8O'><tfoot id='gtB8O'></tfoot><dl id='gtB8O'><fieldset id='gtB8O'></fieldset></dl></div>

                  <legend id='gtB8O'><style id='gtB8O'><dir id='gtB8O'><q id='gtB8O'></q></dir></style></legend>
                    <tbody id='gtB8O'></tbody>
                • 本文介绍了将 char 数组转换为字节数组并再次返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我希望在不创建中间 String 的情况下将 Java char 数组转换为字节数组,因为 char 数组包含密码.我查找了几种方法,但它们似乎都失败了:

                  I'm looking to convert a Java char array to a byte array without creating an intermediate String, as the char array contains a password. I've looked up a couple of methods, but they all seem to fail:

                  char[] password = "password".toCharArray();
                  
                  byte[] passwordBytes1 = new byte[password.length*2];
                  ByteBuffer.wrap(passwordBytes1).asCharBuffer().put(password);
                  
                  byte[] passwordBytes2 = new byte[password.length*2];
                  for(int i=0; i<password.length; i++) {
                      passwordBytes2[2*i] = (byte) ((password[i]&0xFF00)>>8); 
                      passwordBytes2[2*i+1] = (byte) (password[i]&0x00FF); 
                  }
                  
                  String passwordAsString = new String(password);
                  String passwordBytes1AsString = new String(passwordBytes1);
                  String passwordBytes2AsString = new String(passwordBytes2);
                  
                  System.out.println(passwordAsString);
                  System.out.println(passwordBytes1AsString);
                  System.out.println(passwordBytes2AsString);
                  assertTrue(passwordAsString.equals(passwordBytes1) || passwordAsString.equals(passwordBytes2));
                  

                  断言总是失败(而且,关键的是,当代码在生产中使用时,密码被拒绝),但打印语句打印出密码三次.为什么 passwordBytes1AsStringpasswordBytes2AsStringpasswordAsString 不同,但看起来相同?我错过了一个空终止符还是什么?我该怎么做才能使转换和取消转换工作?

                  The assertion always fails (and, critically, when the code is used in production, the password is rejected), yet the print statements print out password three times. Why are passwordBytes1AsString and passwordBytes2AsString different from passwordAsString, yet appear identical? Am I missing out a null terminator or something? What can I do to make the conversion and unconversion work?

                  推荐答案

                  问题是你使用了 String(byte[]) 构造函数,它使用平台默认编码.这几乎是从不你应该做的——如果你传入UTF-16"作为字符编码来工作,你的测试可能会通过.目前我怀疑 passwordBytes1AsStringpasswordBytes2AsString 都是 16 个字符长,其他每个字符都是 U+0000.

                  The problem is your use of the String(byte[]) constructor, which uses the platform default encoding. That's almost never what you should be doing - if you pass in "UTF-16" as the character encoding to work, your tests will probably pass. Currently I suspect that passwordBytes1AsString and passwordBytes2AsString are each 16 characters long, with every other character being U+0000.

                  这篇关于将 char 数组转换为字节数组并再次返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在java中将4字节数组转换为浮点数 下一篇:Java将字符添加到字符串

                  相关文章

                  最新文章

                  <legend id='BuCkW'><style id='BuCkW'><dir id='BuCkW'><q id='BuCkW'></q></dir></style></legend>
                • <i id='BuCkW'><tr id='BuCkW'><dt id='BuCkW'><q id='BuCkW'><span id='BuCkW'><b id='BuCkW'><form id='BuCkW'><ins id='BuCkW'></ins><ul id='BuCkW'></ul><sub id='BuCkW'></sub></form><legend id='BuCkW'></legend><bdo id='BuCkW'><pre id='BuCkW'><center id='BuCkW'></center></pre></bdo></b><th id='BuCkW'></th></span></q></dt></tr></i><div id='BuCkW'><tfoot id='BuCkW'></tfoot><dl id='BuCkW'><fieldset id='BuCkW'></fieldset></dl></div>
                  <tfoot id='BuCkW'></tfoot>

                  <small id='BuCkW'></small><noframes id='BuCkW'>

                        <bdo id='BuCkW'></bdo><ul id='BuCkW'></ul>