【发布时间】:2018-03-29 05:55:25
【问题描述】:
这是迄今为止我的分析和障碍,假设对于下面的字符,“UTF-8”字符集基本支持,“EUC-JP”不支持。 “——” 对于php,有一个方法“var_dump(input_string)”可以将任何字符串转换为编码为“EUC-JP”的字节数组,在这种情况下,它返回,
[161, 189, 10] //Note: [3]=>int(10) for Line Feed.
类似地,当我生成编码为“UTF-8”的字节数组时,在这种情况下它会返回,
[226, 128, 141,10] //Note: [4]=>int(10) for Line Feed.
但是,当我在 Groovy 中尝试同样的事情时, 它的行为完全不同,对于 EUC-JP 的字节排列如下,
[-95, -67, 10] //Note: [3]=>int(10) for Line Feed.
对于 UTF-8,
[-30, -128, -107, 10] //Note: [4]=>int(10) for Line Feed.
注意,我直接从 2 个分别用 EUC-JP 和 UTF-8 编码的不同文本文件中获取数据。上述所有数组的最后一个字节用于 LF(换行)。由于这两种语言的相同字符编码的字节排列不同,因此不可能匹配两者之间产生的哈希。
这是到目前为止的代码示例,从 php 开始,
<?php
$myfile = fopen("euc_jp.txt", "r") or die("Unable to open file!");
$str1 = fread($myfile,filesize("euc_jp.txt"));
echo "Read From File EUC-JP:<br/>";
echo $str1;
$byte_array1 = unpack('C*', $str1);
echo "<br/>Byte Dump of EUC-JP File Content:<br/>";
var_dump($byte_array1);
echo "<br/><br/>";
$myfile2 = fopen("utf_8.txt", "r") or die("Unable to open file!");
$str2 = fread($myfile2,filesize("utf_8.txt"));
echo "<br/><br/>";
echo "Read From File UTF-8:<br/>";
echo $str2;
$byte_array2 = unpack('C*', $str2);
echo "<br/>Byte Dump of EUC-JP File Content:<br/>";
var_dump($byte_array2);
$encodedToEucJp = mb_convert_encoding($str2, "EUC_JP");
echo "<br/><br/>After conversion (UTF-8) to (EUC-JP): <br/>";
echo $encodedToEucJp;
echo "<br/><br/>";
echo "Hash Generation Directly From EUC-JP:<br/>";
print_r(md5($str1));
echo "<br/><br/>";
echo "Hash Generation From UTF-8 File Content After Encoded to EUC-JP:<br/>";
print_r(md5($encodedToEucJp));
fclose($myfile);
fclose($myfile2);
?>
对于 Groovy,
println(new File('/var/www/html/euc_jp.txt').getText('EUC-JP').getBytes("EUC-JP"))
println(new File('/var/www/html/utf_8.txt').getText('UTF-8').getBytes("UTF-8"))
到目前为止,这是我的障碍,首先这两种语言的字节表示不同,如果不是 Groovy 和 Java8 的限制,我如何产生与 php 产生的相同的字节排列,其次,本机 php 函数 b_convert_encoding() 的等效代码是什么?所以,我能够转换任何字符串编码,其中可能有一些字符不支持两种编码机制。
【问题讨论】:
标签: java php groovy utf-8 character-encoding