java - Can someone explain the conversion from byte array to hex string? -


I recently started looking for MD5 hashing (in Java) and I have got to help complete the algorithms and methods, I think how it actually works.

For one, I found out of the following:

  Convert Private Static String ToHex (Byte [] data) {StringBuffer buf = new stringbuffer (); {Int halfbyte = (data [i]>>> 4) & amp; nbsp; (for int i = 0; i & lt; data lamps; i ++) & amp; 0x0F; Int two_halfs = 0; Do it {if (0> = halfbyte) & ampby; (halfbyte & lt; = 9)) buf.append ((four) ('0' + halfbyte)); Else buf.append ((four) ('a' + (halfbyte - 10)); Half-bite = data [i] & amp; 0x0F; } While (two_half ++ & lt; 1); } Return buf.toString (); }  

I do not need to use bit-shifting in Java, so I'm a little disturbing on that enough to explain to someone (in simple words) the above code conversion How does ">>>"?

I also found other solutions on the stack overflow, like, and, which instead uses the bigteger:

  try {string s = "test STRING"; MessageDigest md5 = MessageDigest.getInstance ("MD5"); Md5.update (s.getBytes (), 0, s.length ()); String signature = new bigigner (1, MD5 digest) Tosting (16); System.out.println ("Signature:" + Signature); } Catch (Last Knowledge Algorithm Exposure E) {e.printStackTrace (); }  

Why does this work too, and how is it more efficient?

Thank you for your time. {StringBuffer buf = new StringBuffer ())

private static string convertorhex { ; (Int i = 0; i & lt; data.length; i ++) {

To this point ... just go through the basic set up and all the bytes Start a loop array

  int halfbyte = (data [i]>>> 4) & amp; 0x0F;  

Bytes when converted to hex, depending on the basis on which you see it based on two hex digits or 8 binary digits, the above statement varies for 4 bits (>>> Unsigned rights change) and logical ARDS 0000 1111, so that the result is an integer equivalent to the high 4 bit (first hex digit) of a byte.

Say 23 was an input, it's 0001 0111 in binary. Makes shift and is logical and it is covered up 0000 0001.

  int two_halfs = 0; <{Code  

This sets / sets only for two-time running

  if ((0 <= prefix = halfbyte) & amp; ; Ampby; (halfbyte) & lt; = 9)) buf.append ((four) ('0' + halfbyte)); Else buf.append ((four) ('a' + (halfbyte - 10));  

Here we are displaying actual hex digits, basically just moving zero or one character as the starting point and to the right character. First if the statement covers all digits 0- 9, and the second covers all points 10-15 (air in hex)

Again, example 0000 0001 is equal to 1 in decimal. If we get caught on top, add block and '1' character to the character '1', to get '1' character, go to that string and proceed.

  half-bite = data [i] & amp; 0x0F;  

Now we just make an integer equal to less than bits and repeat.

Then, if our input was 23 ... 0001 0111 becomes logical and only 0000 0111 which is 7 in decimal. Repeat the above logic above and the character '7' is displayed.

 } while (two_half ++ & lt; 1);  

Now we go to the next byte in the array and repeat.

 } return buf.toString (); }  

To answer your next question, see the documentation in Java API that is already a base conversion utility created in BigInterizer.

Do not know the implementation used by the Java API, I can not say for sure, but I can bet that Javascript transmissions have been posted to a somewhat simple algorithm.


Comments