Java中的UTF-16编码与C# [英] UTF-16 Encoding in Java versus C#

查看:112
本文介绍了Java中的UTF-16编码与C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以UTF-16编码方案读取一个字符串,并对其执行MD5散列。但是奇怪的是,当我尝试这样做时,Java和C#返回不同的结果。

I am trying to read a String in UTF-16 encoding scheme and perform MD5 hashing on it. But strangely, Java and C# are returning different results when I try to do it.

以下是 Java 中的代码段:

public static void main(String[] args) {
    String str = "preparar mantecado con coca cola";
    try {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(str.getBytes("UTF-16"));
        byte[] hash = digest.digest();
        String output = "";
        for(byte b: hash){
            output += Integer.toString( ( b & 0xff ) + 0x100, 16).substring( 1 );
        }
        System.out.println(output);
    } catch (Exception e) {

    }
}

输出为: 249ece65145dca34ed310445758e5504

以下是 C#中的代码段:

   public static string GetMD5Hash()
        {
            string input = "preparar mantecado con coca cola";
            System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bs = System.Text.Encoding.Unicode.GetBytes(input);
            bs = x.ComputeHash(bs);
            System.Text.StringBuilder s = new System.Text.StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2").ToLower());
            }
            string output= s.ToString();
            Console.WriteLine(output);
        }

输出为: c04d0f518ba2555977fa1ed7f93ae2b3

我不知道为什么输出不一样。我们如何更改上述代码,以便它们都返回相同的输出?

I am not sure, why the outputs are not the same. How do we change the above piece of code, so that both of them return the same output?

推荐答案

UTF-16!= UTF-16。

UTF-16 != UTF-16.

在Java中, getBytes(UTF-16)返回一个可选的byte-订购商标。 C#的 System.Text.Encoding.Unicode.GetBytes 返回一个小端的表示。我无法从这里检查您的代码,但我认为您需要精确地指定转换。

In Java, getBytes("UTF-16") returns an a big-endian representation with optional byte-ordering mark. C#'s System.Text.Encoding.Unicode.GetBytes returns a little-endian representation. I can't check your code from here, but I think you'll need to specify the conversion precisely.

在Java版本中尝试 getBytes(UTF-16LE)

Try getBytes("UTF-16LE") in the Java version.

这篇关于Java中的UTF-16编码与C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆