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

时间:2021-06-22 15:44:47

I got the problem when convert between this 2 type in PHP. This is the code I searched in google

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

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;
}

I check it and found out this when I use XOR to encrypt.

当我使用XOR加密时,我检查并发现了这个。

I have the string "this is the test", after XOR with a key, I have the result in string ↕↑↔§P↔§P ♫§T↕§↕. After that, I tried to convert it to hex by function strToHex() and I got these 12181d15501d15500e15541215712. Then, I tested with the function hexToStr() and I have ↕↑↔§P↔§P♫§T↕§q. So, what should I do to solve this problem? Why does it wrong when I convert this 2 style value?

我有“这是测试”的字符串,在XOR和一个键之后,我就有了结果,在字符串中,我有了这个结果。在那之后,我尝试用函数strToHex()将它转换为十六进制,我得到了这些12181d15501d15500e15541215712。然后,我用函数hexToStr()进行了测试,我得到了q。那么,我该怎么做才能解决这个问题呢?当我转换这两个样式值时为什么会出错呢?

7 个解决方案

#1


42  

For any char with ord($char) < 16 you get a HEX back which is only 1 long. You forgot to add 0 padding.

对于任何一个字符($char) < 16,您将得到一个只有1个long的十六进制。你忘记加0填充了。

This should solve it:

这应该解决方法:

<?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";

#2


13  

Here's what I use:

以下是我使用:

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

#3


8  

PHP :

PHP:

string to hex:

十六进制字符串:

implode(unpack("H*", $string));

hex to string:

十六进制字符串:

pack("H*", $hex);

#4


1  

function hexToStr($hex){
    // Remove spaces if the hex string has spaces
    $hex = str_replace(' ', '', $hex);
    return hex2bin($hex);
}
// Test it 
$hex    = "53 44 43 30 30 32 30 30 30 31 37 33";
echo hexToStr($hex); // SDC002000173

/**
 * Test Hex To string with PHP UNIT
 * @param  string $value
 * @return 
 */
public function testHexToString()
{
    $string = 'SDC002000173';
    $hex    = "53 44 43 30 30 32 30 30 30 31 37 33";
    $result = hexToStr($hex);

    $this->assertEquals($result,$string);
}

#5


0  

I only have half the answer, but I hope that it is useful as it adds unicode (utf-8) support

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

//decimal to unicode character
function unichr($dec) { 
  if ($dec < 128) { 
    $utf = chr($dec); 
  } else if ($dec < 2048) { 
    $utf = chr(192 + (($dec - ($dec % 64)) / 64)); 
    $utf .= chr(128 + ($dec % 64)); 
  } else { 
    $utf = chr(224 + (($dec - ($dec % 4096)) / 4096)); 
    $utf .= chr(128 + ((($dec % 4096) - ($dec % 64)) / 64)); 
    $utf .= chr(128 + ($dec % 64)); 
  } 
  return $utf;
}

To string

字符串

var_dump(unichr(hexdec('e641')));

Source: http://www.php.net/manual/en/function.chr.php#Hcom55978

来源:http://www.php.net/manual/en/function.chr.php Hcom55978

#6


0  

Using @bill-shirley answer with a little addition

使用@bill-shirley的答案添加一点。

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

Usage:

用法:

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

#7


0  

For people that are end up here and are just looking for the hex representation of a (binary) string.

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

bin2hex("that's all you need");
# 74686174277320616c6c20796f75206e656564

hex2bin('74686174277320616c6c20796f75206e656564');
# that's all you need

#1


42  

For any char with ord($char) < 16 you get a HEX back which is only 1 long. You forgot to add 0 padding.

对于任何一个字符($char) < 16,您将得到一个只有1个long的十六进制。你忘记加0填充了。

This should solve it:

这应该解决方法:

<?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";

#2


13  

Here's what I use:

以下是我使用:

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

#3


8  

PHP :

PHP:

string to hex:

十六进制字符串:

implode(unpack("H*", $string));

hex to string:

十六进制字符串:

pack("H*", $hex);

#4


1  

function hexToStr($hex){
    // Remove spaces if the hex string has spaces
    $hex = str_replace(' ', '', $hex);
    return hex2bin($hex);
}
// Test it 
$hex    = "53 44 43 30 30 32 30 30 30 31 37 33";
echo hexToStr($hex); // SDC002000173

/**
 * Test Hex To string with PHP UNIT
 * @param  string $value
 * @return 
 */
public function testHexToString()
{
    $string = 'SDC002000173';
    $hex    = "53 44 43 30 30 32 30 30 30 31 37 33";
    $result = hexToStr($hex);

    $this->assertEquals($result,$string);
}

#5


0  

I only have half the answer, but I hope that it is useful as it adds unicode (utf-8) support

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

//decimal to unicode character
function unichr($dec) { 
  if ($dec < 128) { 
    $utf = chr($dec); 
  } else if ($dec < 2048) { 
    $utf = chr(192 + (($dec - ($dec % 64)) / 64)); 
    $utf .= chr(128 + ($dec % 64)); 
  } else { 
    $utf = chr(224 + (($dec - ($dec % 4096)) / 4096)); 
    $utf .= chr(128 + ((($dec % 4096) - ($dec % 64)) / 64)); 
    $utf .= chr(128 + ($dec % 64)); 
  } 
  return $utf;
}

To string

字符串

var_dump(unichr(hexdec('e641')));

Source: http://www.php.net/manual/en/function.chr.php#Hcom55978

来源:http://www.php.net/manual/en/function.chr.php Hcom55978

#6


0  

Using @bill-shirley answer with a little addition

使用@bill-shirley的答案添加一点。

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

Usage:

用法:

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

#7


0  

For people that are end up here and are just looking for the hex representation of a (binary) string.

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

bin2hex("that's all you need");
# 74686174277320616c6c20796f75206e656564

hex2bin('74686174277320616c6c20796f75206e656564');
# that's all you need