PHP 将字符串转换为十六进制,将十六进制转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14674834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP convert string to hex and hex to string
提问by JoeNguyen
I got the problem when convert between this 2 type in PHP. This is the code I searched in google
在 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;
}
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?
我有 string "this is the test",在用一个键异或后,我有 string 的结果?↑?§P?§P ?§T?§?。之后,我尝试通过函数 strToHex() 将其转换为十六进制,我得到了这些12181d15501d15500e15541215712. 然后,我用函数 hexToStr() 进行了测试,我有?↑?§P?§P?§T?§q. 那么,我应该怎么做才能解决这个问题?为什么我转换这 2 个样式值时会出错?
回答by Philippe Gerber
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
回答by boomla
For any char with ord($char) < 16 you get a HEX back which is only 1 long. You forgot to add 0 padding.
对于 ord($char) < 16 的任何字符,您会得到一个只有 1 长的十六进制数。您忘记添加 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";
回答by ????
PHP :
PHP :
string to hex:
字符串到十六进制:
implode(unpack("H*", $string));
hex to string:
十六进制到字符串:
pack("H*", $hex);
回答by Bill Shirley
Here's what I use:
这是我使用的:
function strhex($string) {
$hexstr = unpack('H*', $string);
return array_shift($hexstr);
}
回答by Kamaro Lambert
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);
}
回答by Timo Huovinen
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
回答by PeterT
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
回答by Bhargav Venkatesh
You can try the following code to convert the image to hex string
您可以尝试以下代码将图像转换为十六进制字符串
<?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]));
}
}
?>

