2016-07-02 32 views
2

我有以下代碼:轉換列表<Byte>串

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

在這段代碼,當我調試程序檢查的resultresult顯示爲[0,7],而不是07

有沒有辦法將ListByte s轉換爲String而沒有任何問題?

回答

2

在這個解決方案,我收集的人物在一個StringBuilder並轉換爲字符串結尾:

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); 
    } 
+1

好的方法,謝謝。但它不顯示輸出爲07. –

+0

你想要解密什麼字符串輸入? –

+0

我想要做的就是用A2與XOR A2並將07作爲字符串。而已。 –

0

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

這是因爲您要打印的列表作爲String

你可以擺脫那個做這樣的事情:

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

或者,也許,但不是很優雅:

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

是否有一個安全的解決方案:String result = sample.toString(); ?我的意思是還有另一個toString方法嗎? –

+1

字符串是在對象類中定義的,而列表實現它就像那樣...我會使用for循環和某種字符串生成器,如果有的話需要它:) –

-1

轉換你的名單分成數組像下面。感謝@ bcsb1001

T []指定者(T []一個)

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

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

後,您可以從字節數組的字符串。

使用這個構造:

enter image description here

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

有沒有辦法將「列表示例」轉換爲字節數組? –

+1

對不起,沒有工作。它說無法解析構造函數。我想沒有這樣的構造函數。 –

+0

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html –

2

如果你是打開使用第三方庫,以下解決方案使用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中集合提供的基本ByteList,我是能夠避免裝箱。 ByteList上的方法makeString允許您控制分隔符。傳遞一個空的String可以去掉上使用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集合的參與者。