UTF-16 Encoding in Java versus C# -
i trying read string in utf-16 encoding scheme , perform md5 hashing on it. strangely, java , c# returning different results when try it.
the following piece of code in 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) { } }
the output is: 249ece65145dca34ed310445758e5504
the following piece of code in 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); }
the output is: c04d0f518ba2555977fa1ed7f93ae2b3
i not sure, why outputs not same. how change above piece of code, both of them return same output?
utf-16 != utf-16.
in java, getbytes("utf-16")
returns big-endian representation optional byte-ordering mark. c#'s system.text.encoding.unicode.getbytes
returns little-endian representation. can't check code here, think you'll need specify conversion precisely.
try getbytes("utf-16le")
in java version.
Comments
Post a Comment