2016-06-23 10 views
-2
class By 
{ 
public static void main(String args[]) throws InterruptedException { 
String key = "Hello world";// plaintext 
byte[] x = key.getBytes(); 
StringBuilder sb = new StringBuilder(); 
for (int i = 0; i < x.length; i++) { 
int b = (int) x[i]; 
sb.append(Integer.toHexString(b)); 
} 
System.out.println(sb.toString()); 
} 
} 
This program output is: 48656c6c6f20776f726c64 
I need my output to be a 4-by-4 matrix as such: 
         1st row is:: 48 6f 72 00 
         2nd row is:: 65 20 6c 00 
         3rd row is:: 6c 77 64 00 
         4th row is:: 6c 6f 00 00 

Wie schreibe ich Code dafür in Java? Bitte zeigen Sie mir den Code, um meine benötigte Ausgabe zu bekommen.Wie formatiere ich meine Ausgabe als 4-by-4-Matrix?

+0

Dieselbe Frage buchstäblich in der Lage sein, wurde . https://stackoverflow.com/questions/37999956/how-to-get-hexadecimal-values-from-string-and-form-a-4-by-4-matrix. – bcsb1001

+0

Ich habe gerade gesehen. Aber niemand gab den Code für diese Frage. – devi

+0

Die vorherige Frage wurde gestellt und richtig formatiert .. jetzt ist es zurück zu 0 – Sid

Antwort

0

So etwas ...

public class FourByFour { 

    private List<String> hexlify(final String plaintext) { 
     byte[] x = plaintext.getBytes(); 
     final List<String> list = new ArrayList<>(); 
     for (final byte aX : x) { 
      int b = (int) aX; 
      list.add(Integer.toHexString(b)); 
     } 
     return list; 
    } 

    private void fourByFour(final List<String> hexString) { 
     /* 
      1st row is:: 48 6f 72 00 
      2nd row is:: 65 20 6c 00 
      3rd row is:: 6c 77 64 00 
      4th row is:: 6c 6f 00 00 
     */ 
     for (int x = 0; x < 4; x++) { 
      for (int y = x; y < hexString.size(); y += 4) { 
       System.out.print(hexString.get(y) + " "); 
      } 
      System.out.println("00"); 
     } 
    } 

    public static void main(String args[]) throws InterruptedException { 
     final FourByFour fourByFour = new FourByFour(); 
     final List<String> hexString = fourByFour.hexlify("Hello World"); 
     fourByFour.fourByFour(hexString); 
    } 
} 

Ausgänge:

hexString = [48, 65, 6c, 6c, 6f, 20, 57, 6f, 72, 6c, 64] 
48 6f 72 00 
65 20 6c 00 
6c 57 64 00 
6c 6f 00 

Ich denke, Sie sollten nur gebeten, den Rest zu tun ...