久久久久久久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 數(shù)組轉(zhuǎn)換為字節(jié)數(shù)組并再次返回

      Converting char array into byte array and back again(將 char 數(shù)組轉(zhuǎn)換為字節(jié)數(shù)組并再次返回)
      <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 數(shù)組轉(zhuǎn)換為字節(jié)數(shù)組并再次返回的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                我希望在不創(chuàng)建中間 String 的情況下將 Java char 數(shù)組轉(zhuǎn)換為字節(jié)數(shù)組,因為 char 數(shù)組包含密碼.我查找了幾種方法,但它們似乎都失敗了:

                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));
                

                斷言總是失敗(而且,關(guān)鍵的是,當代碼在生產(chǎn)中使用時,密碼被拒絕),但打印語句打印出密碼三次.為什么 passwordBytes1AsStringpasswordBytes2AsStringpasswordAsString 不同,但看起來相同?我錯過了一個空終止符還是什么?我該怎么做才能使轉(zhuǎn)換和取消轉(zhuǎn)換工作?

                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[]) 構(gòu)造函數(shù),它使用平臺默認編碼.這幾乎是從不你應(yīng)該做的——如果你傳入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.

                這篇關(guān)于將 char 數(shù)組轉(zhuǎn)換為字節(jié)數(shù)組并再次返回的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                相關(guān)文檔推薦

                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 操作數(shù)的三元表達式的類型是什么?)
                Read a text file and store every single character occurrence(讀取文本文件并存儲出現(xiàn)的每個字符)
                Why do I need to explicitly cast char primitives on byte and short?(為什么我需要在 byte 和 short 上顯式轉(zhuǎn)換 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>
                          主站蜘蛛池模板: 国产精品一区在线 | 国产欧美一区二区三区久久手机版 | 久久国产精品99久久久大便 | 国产精品免费一区二区三区四区 | 亚洲国产日韩欧美 | 精品国产精品三级精品av网址 | 国产精品美女久久久久久久网站 | 日韩在线看片 | 在线欧美小视频 | 国精产品一区一区三区免费完 | 日韩欧美一区二区三区免费观看 | 亚洲性人人天天夜夜摸 | 久草资源 | 午夜电影网站 | 国产欧美一区二区三区久久人妖 | 国产成人99| 操人网站| 日韩欧美视频网站 | 中文在线亚洲 | 亚洲国产欧美精品 | 宅女噜噜66国产精品观看免费 | 成人久久 | 91视频国产区 | 精品亚洲91 | 中文字幕视频在线观看免费 | 久草福利| 人人九九 | 日韩欧美精品一区 | 日韩精品在线看 | 亚洲人成免费 | 成人av播放 | 国产特级毛片 | 成人福利在线观看 | 黑人久久久 | 欧美色影院 | 成人在线视频免费观看 | 亚洲国产一区二区三区在线观看 | 亚洲精品福利在线 | 精品无码久久久久久国产 | 久久久精品影院 | 久久精品色欧美aⅴ一区二区 |