Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.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
PHP将字符串转换为十六进制和十六进制转换为字符串_Php_String_Hex - Fatal编程技术网

PHP将字符串转换为十六进制和十六进制转换为字符串

PHP将字符串转换为十六进制和十六进制转换为字符串,php,string,hex,Php,String,Hex,我在PHP中转换这2种类型时遇到了问题。这是我在谷歌搜索的代码 function strToHex($string){ $hex=''; for ($i=0; $i < strlen($string); $i++){ $hex .= dechex(ord($string[$i])); } return $hex; } function hexToStr($hex){ $string=''; for ($i=0; $i &l

我在PHP中转换这2种类型时遇到了问题。这是我在谷歌搜索的代码

function strToHex($string){
    $hex='';
    for ($i=0; $i < strlen($string); $i++){
        $hex .= dechex(ord($string[$i]));
    }
    return $hex;
}


function hexToStr($hex){
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2){
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
}
函数strothex($string){
$hex='';
对于($i=0;$i
我检查了它,在使用XOR加密时发现了这一点

我有字符串
“这是测试”
,在使用键进行异或后,我在字符串
↕↑↔§P↔§P♫§T↕§↕。在那之后,我尝试通过函数strothex()将其转换为hex,得到了这些
12181d15501d15500e15541215712
。然后,我使用函数hexToStr()进行了测试,得到了
↕↑↔§P↔§P♫§T↕§q
。那么,我该怎么解决这个问题呢?为什么我转换这个2样式值时会出错?

对于ord($char)<16的任何字符,都会得到一个只有1长的十六进制。您忘记添加0填充

这应该解决这个问题:

<?php
function strToHex($string){
    $hex = '';
    for ($i=0; $i<strlen($string); $i++){
        $ord = ord($string[$i]);
        $hexCode = dechex($ord);
        $hex .= substr('0'.$hexCode, -2);
    }
    return strToUpper($hex);
}
function hexToStr($hex){
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2){
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
}


// Tests
header('Content-Type: text/plain');
function test($expected, $actual, $success) {
    if($expected !== $actual) {
        echo "Expected: '$expected'\n";
        echo "Actual:   '$actual'\n";
        echo "\n";
        $success = false;
    }
    return $success;
}

$success = true;
$success = test('00', strToHex(hexToStr('00')), $success);
$success = test('FF', strToHex(hexToStr('FF')), $success);
$success = test('000102FF', strToHex(hexToStr('000102FF')), $success);
$success = test('↕↑↔§P↔§P ♫§T↕§↕', hexToStr(strToHex('↕↑↔§P↔§P ♫§T↕§↕')), $success);

echo $success ? "Success" : "\nFailed";

我只有一半的答案,但我希望它是有用的,因为它增加了unicode(utf-8)支持

来源:

以下是我使用的:

function strhex($string) {
  $hexstr = unpack('H*', $string);
  return array_shift($hexstr);
}
PHP:

字符串到十六进制:

implode(unpack("H*", $string));
十六进制到字符串:

pack("H*", $hex);

使用@bill shirley answer并添加一点内容

function str_to_hex($string) {
$hexstr = unpack('H*', $string);
return array_shift($hexstr);
}
function hex_to_str($string) {
return hex2bin("$string");
}
用法:

  $str = "Go placidly amidst the noise";
  $hexstr = str_to_hex($str);// 476f20706c616369646c7920616d6964737420746865206e6f697365
  $strstr = hex_to_str($str);// Go placidly amidst the noise

对于在这里结束的人,他们只是在寻找(二进制)字符串的十六进制表示形式


文档:,。

您可以尝试使用以下代码将图像转换为十六进制字符串

<?php
$image = 'sample.bmp';
$file = fopen($image, 'r') or die("Could not open $image");
while ($file && !feof($file)){
$chunk = fread($file, 1000000); # You can affect performance altering
this number. YMMV.
# This loop will be dog-slow, almost for sure...
# You could snag two or three bytes and shift/add them,
# but at 4 bytes, you violate the 7fffffff limit of dechex...
# You could maybe write a better dechex that would accept multiple bytes
# and use substr... Maybe.
for ($byte = 0; $byte < strlen($chunk); $byte++)){
echo dechex(ord($chunk[$byte]));
}
}
?>

您知道PHP中有
hex2bin()
bin2hex()
?strothex返回一个十六进制字符串-因此,如果直接使用
^
运算符对其进行异或,将不会给出任何好的结果。也许您可以给strothex另一个参数,该参数是您想要与之异或的数字,并直接在该函数中进行异或:
$hex.=dechex(ord($string[$i])^$MYKEYBYTE)我认为问题出在hexToStr()函数上。因为当它转换为字符串时,它会传递空格或某些特殊字符,并使问题出现。我尝试了hex2bin()和bin2hex()。这真的很好,解决了这个问题。但在现实情况下,情况并非如此。如果我在用XOR键对明文进行加密后调用bin2hex()函数,那就对了。但在实际情况中,我们通常在XOR之后使用strToHex(),因此当我们使用XOR对带有密钥的密码进行解密以获得明文时,我们调用hexToStr(),它将得到错误的结果
如果是16位,我应该怎么做?您可以使用
list
,即:
list(,$hex)=unpack('H*',$string),避免调用内爆64的十六进制值为40。但是它给了我一些其他的东西为什么不先转换重音字符呢?有什么特别推荐的方法吗<代码>mb_转换_编码
?这超出了原始范围,但十六进制信息将在MSSQL中转换回字符串,MSSQL没有解码功能。with?怎么样?
function str_to_hex($string) {
$hexstr = unpack('H*', $string);
return array_shift($hexstr);
}
function hex_to_str($string) {
return hex2bin("$string");
}
  $str = "Go placidly amidst the noise";
  $hexstr = str_to_hex($str);// 476f20706c616369646c7920616d6964737420746865206e6f697365
  $strstr = hex_to_str($str);// Go placidly amidst the noise
bin2hex("that's all you need");
# 74686174277320616c6c20796f75206e656564

hex2bin('74686174277320616c6c20796f75206e656564');
# that's all you need
<?php
$image = 'sample.bmp';
$file = fopen($image, 'r') or die("Could not open $image");
while ($file && !feof($file)){
$chunk = fread($file, 1000000); # You can affect performance altering
this number. YMMV.
# This loop will be dog-slow, almost for sure...
# You could snag two or three bytes and shift/add them,
# but at 4 bytes, you violate the 7fffffff limit of dechex...
# You could maybe write a better dechex that would accept multiple bytes
# and use substr... Maybe.
for ($byte = 0; $byte < strlen($chunk); $byte++)){
echo dechex(ord($chunk[$byte]));
}
}
?>