-
Printing Byte Array
When I am running the code as follows:
BufferedReader userInput = new BufferedReader (new InputStreamReader(System.in));
System.out.println("Enter String: ");
String input = userInput.readLine();
System.out.println("UserInput:"+input);
byte[] sha1hash = input.getBytes();
System.out.println("ByteFormat:"+sha1hash);
String t = new String( sha1hash);
System.out.println("StringFormat:"+t);
for (int i = 0; i < sha1hash.length; i++)
System.out.println("ByteArray["+i+"]:"+sha1hash[i]);
I am getting the output as follows:
Enter String:
23
UserInput:23
ByteFormat:[B@10b62c9
StringFormat:23
ByteArray[0]:50
ByteArray[1]:51
I am unable to understand that when I am printing each element of the byte array where from it is getting those values.
-
i'm not sure what your question is,
if you said you want to get the output(byte) which same with your input, you may try this.
hope this is helpful
-
My question is what are those values that it's printing like 50, 51
-
these are the unicode values, you need to cast it to char to get the corresponding character
for (int i = 0; i < sha1hash.length; i++){
System.out.println("ByteArray["+i+"]:"+sha1hash[i]);
System.out.println("character value is "+(char)sha1hash[i]);
}