2016-07-02 28 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集合的参与者。