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

u91tlkcl  于 2021-07-26  发布在  Java
关注(0)|答案(8)|浏览(581)

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

  1. function strToHex($string){
  2. $hex='';
  3. for ($i=0; $i < strlen($string); $i++){
  4. $hex .= dechex(ord($string[$i]));
  5. }
  6. return $hex;
  7. }
  8. function hexToStr($hex){
  9. $string='';
  10. for ($i=0; $i < strlen($hex)-1; $i+=2){
  11. $string .= chr(hexdec($hex[$i].$hex[$i+1]));
  12. }
  13. return $string;
  14. }

我检查它,发现这是当我使用异或加密。
我有绳子 "this is the test" ,在用一个键进行异或之后,我得到了字符串形式的结果 ↕↑↔§P↔§P ♫§T↕§↕ . 在那之后,我试着用strtohex()函数把它转换成hex 12181d15501d15500e15541215712 . 然后,我测试了hextostr()函数 ↕↑↔§P↔§P♫§T↕§q . 那么,我该怎么解决这个问题呢?为什么在转换这个2样式值时会出错?

krcsximq

krcsximq1#

对于在这里结束并且只是寻找(二进制)字符串的十六进制表示的人。

  1. bin2hex("that's all you need");
  2. # 74686174277320616c6c20796f75206e656564
  3. hex2bin('74686174277320616c6c20796f75206e656564');
  4. # that's all you need

文件编号:bin2hex,hex2bin。

tvokkenx

tvokkenx2#

对于ord($char)<16的任何字符,您将得到一个长度仅为1的十六进制back。您忘记添加0填充。
这应该可以解决:

  1. <?php
  2. function strToHex($string){
  3. $hex = '';
  4. for ($i=0; $i<strlen($string); $i++){
  5. $ord = ord($string[$i]);
  6. $hexCode = dechex($ord);
  7. $hex .= substr('0'.$hexCode, -2);
  8. }
  9. return strToUpper($hex);
  10. }
  11. function hexToStr($hex){
  12. $string='';
  13. for ($i=0; $i < strlen($hex)-1; $i+=2){
  14. $string .= chr(hexdec($hex[$i].$hex[$i+1]));
  15. }
  16. return $string;
  17. }
  18. // Tests
  19. header('Content-Type: text/plain');
  20. function test($expected, $actual, $success) {
  21. if($expected !== $actual) {
  22. echo "Expected: '$expected'\n";
  23. echo "Actual: '$actual'\n";
  24. echo "\n";
  25. $success = false;
  26. }
  27. return $success;
  28. }
  29. $success = true;
  30. $success = test('00', strToHex(hexToStr('00')), $success);
  31. $success = test('FF', strToHex(hexToStr('FF')), $success);
  32. $success = test('000102FF', strToHex(hexToStr('000102FF')), $success);
  33. $success = test('↕↑↔§P↔§P ♫§T↕§↕', hexToStr(strToHex('↕↑↔§P↔§P ♫§T↕§↕')), $success);
  34. echo $success ? "Success" : "\nFailed";
展开查看全部
tv6aics1

tv6aics13#

PHP:
字符串到十六进制:

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

十六进制到字符串:

  1. pack("H*", $hex);
zqry0prt

zqry0prt4#

我用的是:

  1. function strhex($string) {
  2. $hexstr = unpack('H*', $string);
  3. return array_shift($hexstr);
  4. }
gzszwxb4

gzszwxb45#

  1. function hexToStr($hex){
  2. // Remove spaces if the hex string has spaces
  3. $hex = str_replace(' ', '', $hex);
  4. return hex2bin($hex);
  5. }
  6. // Test it
  7. $hex = "53 44 43 30 30 32 30 30 30 31 37 33";
  8. echo hexToStr($hex); // SDC002000173
  9. /**
  10. * Test Hex To string with PHP UNIT
  11. * @param string $value
  12. * @return
  13. */
  14. public function testHexToString()
  15. {
  16. $string = 'SDC002000173';
  17. $hex = "53 44 43 30 30 32 30 30 30 31 37 33";
  18. $result = hexToStr($hex);
  19. $this->assertEquals($result,$string);
  20. }
展开查看全部
fjaof16o

fjaof16o6#

使用@bill shirley answer加上一点附加

  1. function str_to_hex($string) {
  2. $hexstr = unpack('H*', $string);
  3. return array_shift($hexstr);
  4. }
  5. function hex_to_str($string) {
  6. return hex2bin("$string");
  7. }

用法:

  1. $str = "Go placidly amidst the noise";
  2. $hexstr = str_to_hex($str);// 476f20706c616369646c7920616d6964737420746865206e6f697365
  3. $strstr = hex_to_str($str);// Go placidly amidst the noise
pxiryf3j

pxiryf3j7#

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

  1. //decimal to unicode character
  2. function unichr($dec) {
  3. if ($dec < 128) {
  4. $utf = chr($dec);
  5. } else if ($dec < 2048) {
  6. $utf = chr(192 + (($dec - ($dec % 64)) / 64));
  7. $utf .= chr(128 + ($dec % 64));
  8. } else {
  9. $utf = chr(224 + (($dec - ($dec % 4096)) / 4096));
  10. $utf .= chr(128 + ((($dec % 4096) - ($dec % 64)) / 64));
  11. $utf .= chr(128 + ($dec % 64));
  12. }
  13. return $utf;
  14. }

到字符串

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

资料来源:http://www.php.net/manual/en/function.chr.php#hcom55978

展开查看全部
ecr0jaav

ecr0jaav8#

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

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

相关问题