Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVAs HexUtils.bytesToHex()的PHP等价物_Java_Php_Character Encoding - Fatal编程技术网

JAVAs HexUtils.bytesToHex()的PHP等价物

JAVAs HexUtils.bytesToHex()的PHP等价物,java,php,character-encoding,Java,Php,Character Encoding,我需要在md5中编码一个字符串,然后它的字节到2个字符的十六进制编码 我有一些简短的JAVA代码片段,需要将其转换为PHP // Condensed the message and do MD5 try { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] data = cleartext.getBytes(ENCODING); md.update(data); byte[] diges

我需要在md5中编码一个字符串,然后它的字节到2个字符的十六进制编码

我有一些简短的JAVA代码片段,需要将其转换为PHP

  // Condensed the message and do MD5
  try {
    MessageDigest md = MessageDigest.getInstance("MD5");
    byte[] data = cleartext.getBytes(ENCODING);
    md.update(data);
    byte[] digestedByteArray = md.digest();
    // Convert digested bytes to 2 chars Hex Encoding
    md5String = HexUtils.bytesToHex(digestedByteArray);

  } catch (NoSuchAlgorithmException ns) {
    ns.printStackTrace();
  } catch (UnsupportedEncodingException ex) {
    ex.printStackTrace();
  }
我不太确定如何在PHP中实现这一点<代码>编码是
UTF-8

我在PHP中拥有的是

$md5String = md5($clearText);
$hexString = "";

for($i = 0; $i < strlen($md5String); $i++) {
  $hexString .= dechex(ord($md5String[$i]));
}
$md5String=md5($clearText);
$hexString=“”;
对于($i=0;$i
但是输出是不同的,所以我认为十六进制转换失败了

那么,这个JAVA代码段到PHP的正确端口是什么呢?

的输出已经是一个十六进制编码的字符串,因此您无需执行其他操作(只要不将可选的第二个参数设置为
true

$hexString = md5($clearText);