Converting toHexString and MessageDigest in Java to c# equivalent

844 Views Asked by At

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

There are 1 best solutions below

0
Péter Csajtai On

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.