我在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;
}
我检查它并在使用XOR加密时发现了这一点。
我有字符串"this is the test"
,在带有键的XOR之后,我的结果是字符串↕↑↔§P↔§P ♫§T↕§↕
。之后,我尝试通过函数strToHex()将其转换为十六进制,我得到了这些12181d15501d15500e15541215712
。然后,我使用函数hexToStr()测试,我有↕↑↔§P↔§P♫§T↕§q
。那么,我该怎么做才能解决这个问题呢?当我转换这个2样式值时为什么会出错?
答案 0 :(得分:46)
对于任何带有ord的字符($ char)&lt; 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";
答案 1 :(得分:22)
PHP:
字符串到十六进制:
implode(unpack("H*", $string));
hex to string:
pack("H*", $hex);
答案 2 :(得分:19)
对于那些在这里结束并且只是寻找(二进制)字符串的十六进制表示的人。
ngOnInit(){}
答案 3 :(得分:13)
这是我使用的:
function strhex($string) {
$hexstr = unpack('H*', $string);
return array_shift($hexstr);
}
答案 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)
我只有一半的答案,但我希望它有用,因为它增加了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')));
答案 6 :(得分:0)
使用@ bill-shirley回答添加一点
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
答案 7 :(得分:0)
您可以尝试以下代码将图片转换为十六进制字符串
<?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]));
}
}
?>