2013-02-03 477 views
24

我在PHP中使用这两种类型之间的转换时遇到了问题。这是我在谷歌搜索代码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; 
} 

我检查它,并发现这一点,当我使用异或加密。

我有字符串"this is the test",在与一个键异或之后,我得到了字符串↕↑↔§P↔§P ♫§T↕§↕的结果。之后,我尝试通过函数strToHex()将其转换为十六进制,并且我得到了这些12181d15501d15500e15541215712。然后,我用函数hexToStr()进行测试,我有↕↑↔§P↔§P♫§T↕§q。那么,我该怎么做才能解决这个问题呢?为什么当我转换这个2样式值时会出错?

+4

你知道有'HEX2BIN()'和'BIN2HEX()'在PHP? – SparKot

+0

* strToHex *返回一个十六进制的*字符串*,所以如果直接使用'^'运算符进行XOR运算,则不会给出任何好结果。也许你可以给* strToHex *另一个参数,它是你想和XOR进行比较的数字,XOR直接在该函数中:'$ hex。= dechex(ord($ string [$ i])^ $ MYKEYBYTE);' –

+0

我想问题在于hexToStr()函数。因为当它转换为字符串时,它传递空格或一些特殊字符,并使问题 – JoeNguyen

回答

41

对于任何带ord的字符($ char)< 16您将得到一个只有1长的HEX。你忘了添加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"; 
+0

在strToHex函数中,作为整个dechex()和substr -2的替代,可以使用:'$ hex。= sprintf('%02.x',$ ord);' – tropicalm

+0

它不适用于UTF -8个字符 –

0

我只有一半的答案,但我希望它是有用的,因为它增加的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; 
} 

要串

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

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

13

这是我使用:

function strhex($string) { 
    $hexstr = unpack('H*', $string); 
    return array_shift($hexstr); 
} 
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); 
} 
3

PHP:

字符串为十六进制:

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

十六进制字符串:

pack("H*", $hex); 
0

使用@法案 - 雪莉回答一点点补充

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 
相关问题