Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default 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.

  2. #2
    angelicsign is offline Member
    Join Date
    Dec 2008
    Posts
    25
    Rep Power
    0

    Default

    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.
    Java Code:
    (char)sha1hash[i];
    hope this is helpful

  3. #3
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default

    My question is what are those values that it's printing like 50, 51

  4. #4
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    7

    Default

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

Similar Threads

  1. Receive uploaded image / video (byte array) from j2me on jsp.HOW???
    By angelicsign in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-29-2010, 07:21 PM
  2. Byte Array
    By sandor in forum New To Java
    Replies: 12
    Last Post: 01-15-2009, 04:31 AM
  3. Replies: 21
    Last Post: 11-05-2008, 03:31 PM
  4. Replies: 1
    Last Post: 07-04-2008, 06:39 PM
  5. Reading/Writing a File using byte array
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-16-2008, 11:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •