How does a person learn Java? (convert byte array to hexadecimal string)

I know this sounds like a broad question, but I can narrow it down with an example. I am VERY new in Java. For one of my training projects, I wanted to create my own MD5 hash file for use by us. I started very simply, trying to hash the string, and then go to the file later. I created the MD5Hasher.java file and wrote the following:

import java.security.*;
import java.io.*;
public class MD5Hasher{
    public static void main(String[] args){
        String myString = "Hello, World!";
        byte[] myBA = myString.getBytes();
        MessageDigest myMD;
        try{
            myMD = MessageDigest.getInstance("MD5");
            myMD.update(myBA);
            byte[] newBA = myMD.digest();
            String output = newBA.toString();
            System.out.println("The Answer Is: " + output);
        } catch(NoSuchAlgorithmException nsae){
            // print error here
        }
    }
}

java.sun.com, javadocs java.security, , MessageDigest. , getInstance MessageDigest, . Javadoc : " ". , , , . : " , , , ". , , . "toString" , , . , , , , :

: [B @4cb162d5

StackOverflow :

MD5?

:

String plaintext = 'your text here';
MessageDigest m = MessageDigest.getInstance("MD5");
m.reset();
m.update(plaintext.getBytes());
byte[] digest = m.digest();
BigInteger bigInt = new BigInteger(1,digest);
String hashtext = bigInt.toString(16);
// Now we need to zero pad it if you actually want the full 32 chars.
while(hashtext.length() < 32 ){
    hashtext = "0"+hashtext;
}

, , , "BigInteger", .

, , , , , "BigInteger"? , toString newBA , , -, . , Java? C, Java- . , , "" Googling, - ?

, .: -)

+5
11

. , . . , toString [B@somewhere, .

BigInteger .

:

  • BigInteger ( -
  • BigInteger String (, , ) , base 16 (, hex)

while 0-, 32. , , String.format , , :)

+2

, , "", . . , , "" . , .

. /, " " . , ( ) , ( ). - . " " , . . " java".


, . , , 0x10, , ( 0x10!).

StringBuilder hex = new StringBuilder(bytes.length * 2);
for (byte b : bytes) {
    if ((b & 0xff) < 0x10) hex.append("0");
    hex.append(Integer.toHexString(b & 0xff));
}
String hexString = hex.toString();

@extraneon, new BigInteger(byte[]) . . ( ) Java . . byte Java -128 127, 0 255, . , . & 0xff .

, new BigInteger(bytes).toString(16), MD5, MD5, . , MD5.

+6

MessageDigests -, , (, 1f3870be274f6c49b3e31a0c6728957f), .

MessageDigest.toString(), MessageDigest.digest().toString(), Java toString byte[] ( MessageDigest.digest()) , .

( BigInteger, ), , String.

-, , (128- http://en.wikipedia.org/wiki/MD5), , MD5 -10, -2 ( ) , , -16.

+2

, Java MD5, Sun Java Tutorials Java. , Java.

, , , .

+1

BigInteger , , , int long. , , . :

String output = newBA.toString();

:

String output = Arrays.toString(newBA);

, .

+1

IDE, , toString(). Object . toString, , .

0

, toString newBA- , , -, . , Java?

Java , / . 10 , "! , !" - , , .

, , , toString() / , . toString() (javadoc):

. toString , " " . , , . , .

toString Object , , , at-sign `@' - . , , :

getClass(). getName() + '@' + Integer.toHexString(hashCode())

0

. " Java" by David Bishop. , ..

0

, "" - ?

MD5! , , , , MD5.

, Java.

main() : MD5Hasher, . , , " " (, ) .

, ( , - ), , , . .

0

, Java? C, Java- . , "" Googling, - ?

- 1-, ( imo) 2- .

, . Java , Java-. , , , .

0

All Articles