久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <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 class="qwawimqqmiuu" id='gtB8O'><tfoot id='gtB8O'></tfoot><dl id='gtB8O'><fieldset id='gtB8O'></fieldset></dl></div>

    1. <legend id='gtB8O'><style id='gtB8O'><dir id='gtB8O'><q id='gtB8O'></q></dir></style></legend>
    2. 將 char 數組轉換為字節數組并再次返回

      Converting char array into byte array and back again(將 char 數組轉換為字節數組并再次返回)
      <legend id='CIYDh'><style id='CIYDh'><dir id='CIYDh'><q id='CIYDh'></q></dir></style></legend>
    3. <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 class="qwawimqqmiuu" 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>
                  <tbody id='CIYDh'></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模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                quot;Char cannot be dereferencedquot; error(“Char 不能被取消引用錯誤)
                Java Switch Statement - Is quot;orquot;/quot;andquot; possible?(Java Switch 語句 - 是“或/“和可能的?)
                Java Replace Character At Specific Position Of String?(Java替換字符串特定位置的字符?)
                What is the type of a ternary expression with int and char operands?(具有 int 和 char 操作數的三元表達式的類型是什么?)
                Read a text file and store every single character occurrence(讀取文本文件并存儲出現的每個字符)
                Why do I need to explicitly cast char primitives on byte and short?(為什么我需要在 byte 和 short 上顯式轉換 char 原語?)
                <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 class="qwawimqqmiuu" 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'>

                    <tbody id='BuCkW'></tbody>

                          <bdo id='BuCkW'></bdo><ul id='BuCkW'></ul>
                          主站蜘蛛池模板: 大伊人久久| 久久久人| 视频在线观看亚洲 | 成人片免费看 | 久久久久久国 | 国产亚洲精品精品国产亚洲综合 | 精品自拍视频 | 久久黄网 | 谁有毛片 | 久久精品免费一区二区三 | 日韩久久久久久 | 日韩 欧美 综合 | 自拍偷拍亚洲一区 | 亚洲一区二区免费看 | 久久综合九九 | 国产精品一区久久久 | 国精产品一区一区三区免费完 | 国产成人免费视频 | 天天草av| 国产一区二区三区 | 伊人网一区 | 精品久久久久国产 | 91免费电影 | 亚洲精品久久久久久久久久久 | 久久精品国产一区 | 欧美黑人国产人伦爽爽爽 | 国产中文字幕在线观看 | 日韩视频 中文字幕 | 欧美日产国产成人免费图片 | 国产精品黄视频 | 播放一级黄色片 | 2018天天干天天操 | 国产69精品久久久久777 | 成人精品视频在线观看 | 97国产一区二区精品久久呦 | 亚洲视频一区二区三区 | 亚洲精品视频在线 | 成人毛片视频免费 | 国产清纯白嫩初高生视频在线观看 | 一区二区三区精品视频 | 四虎影|