0

I have problem converting the following Java code into toHexString and MessageDigest equivalent to C#.net code. I spend much time in converting them, but seems the result can never be the same.

Please help me especially in toHexString part.

import java.security.MessageDigest;
import java.util.Arrays;

public class Test
{
    public static void main(String[] args) throws Exception
    {
        String keyString = "01100880200013048720181107174008PC".toString();
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(keyString.getBytes("utf-8"));

        byte[] temp = md5.digest("".getBytes("utf-8"));

        String result = "";
        for (int i = 0; i < temp.length; ++i) {
            result = result + Integer.toHexString(0xFF & temp[i] | 0xFFFFFF00).substring(6);
        }
        System.out.println(result);
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
eric
  • 147
  • 2
  • 4
  • 12
  • Possible duplicate of [Calculate a MD5 hash from a string](https://stackoverflow.com/questions/11454004/calculate-a-md5-hash-from-a-string) – Sean Bright Nov 07 '18 at 16:14
  • If interested to convert byte array to hexadecimal, here's a good topic: https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa – Luca Corradi Nov 07 '18 at 16:29

1 Answers1

1

You can get the hexadecimal string representation of an int by calling the .ToString("X2") method on it. Here is the documentation for more options.

To get the first 6 chars you can call the .SubString(0, 6) on the hex value.

Péter Csajtai
  • 878
  • 6
  • 9