簡體   English   中英

轉換清單 <Byte> 串

[英]Convert List<Byte> to string

我有以下代碼:

List<String> decryptedPasswordInPairs = new ArrayList<String>();
String A5 = "A5";
for (String oddPair : oddPairsInEncryptedPassword) {
    List<Byte> sample = new ArrayList<>();
    for (int i = 0; i < oddPair.length(); i++) {
        byte x = (byte) (oddPair.charAt(i) ^ A5.charAt(i % A5.length()));
        sample.add(x);
    }
    String result = sample.toString();
    decryptedPasswordInPairs.add(result);
}

在此代碼中,當我調試程序以檢查result的值時, result顯示為[0,7]而不是07

有沒有辦法將ByteList轉換為String而不出現任何問題?

在此解決方案中,我將字符收集在StringBuilder中,最后將其轉換為String:

List<String> decryptedPasswordInPairs = new ArrayList<String>();
    String A5 = "A5";

    List<String> oddPairsInEncryptedPassword = new LinkedList<String>();
    oddPairsInEncryptedPassword.add("A2");

    for (String oddPair : oddPairsInEncryptedPassword) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < oddPair.length(); i++) {
            byte x = (byte) (oddPair.charAt(i) ^ A5.charAt(i % A5.length()));
            sb.append(""+x);
        }

        String result = sb.toString();
        System.out.println(result);
        decryptedPasswordInPairs.add(result);
    }

如果您願意使用第三方庫,則可以使用Eclipse Collections使用以下解決方案:

MutableList<String> decryptedPasswordInPairs = Lists.mutable.with("A2")
    .collectWith((oddPair, A5) ->
            CharAdapter.adapt(oddPair)
                .injectIntoWithIndex(
                    ByteLists.mutable.empty(),
                    (bytes, character, index) ->
                        bytes.with((byte) (character ^ A5.charAt(index % A5.length()))))
                .makeString(""), "A5");
decryptedPasswordInPairs.each(System.out::println);

我使用了以下導入:

import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.factory.primitive.ByteLists;
import org.eclipse.collections.impl.string.immutable.CharAdapter;

通過使用Eclipse Collections中可用的原始ByteList ,我可以避免裝箱。 makeString上可用的ByteList允許您控制分隔符。 傳遞一個空的String會消除您在List上使用toString引起的逗號。 CharAdapter類圍繞String提供了一組基於char的協議,包括我在此處使用的名為injectIntoWithIndex的方法。

該代碼也可以分成較小的步驟,以使其密度略低,並且可能更易於閱讀。

MutableList<String> decryptedPasswordInPairs = Lists.mutable.with("A2")
    .collect(CharAdapter::adapt)
    .collectWith((oddPair, A5) ->
        oddPair.injectIntoWithIndex(
            ByteLists.mutable.empty(),
            (bytes, character, index) ->
                bytes.with((byte) (character ^ A5.charAt(index % A5.length())))), "A5")
    .collectWith(ByteList::makeString, "");
decryptedPasswordInPairs.each(System.out::println);

注意:我是Eclipse Collections的提交者。

result顯示為[0,7]而不是07

這是因為您將列表打印為String

您可以執行以下操作來擺脫這種情況:

for (Byte b : sample) {
    System.out.print(b);
}

也許,但不是很優雅:

String result = sample.toString().replace(",", "").replace("[", "").replace("]", "");
decryptedPasswordInPairs.add(result);

將您的列表轉換成如下數組。 謝謝@ bcsb1001

T [] toArray(T [] a)

返回一個數組,該數組按適當順序(從第一個元素到最后一個元素)包含此列表中的所有元素; 返回數組的運行時類型是指定數組的運行時類型。 如果列表適合指定的數組,則將其返回。 否則,將使用指定數組的運行時類型和此列表的大小分配一個新數組。

Byte[] byteArray = sample.toArray(new Byte[0])  

之后,您可以從Byte數組創建String。

使用此構造函數:

在此處輸入圖片說明

String str= new String(byteArray, "UTF-8");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM