Home > Backend Development > PHP Problem > How to convert string to hexadecimal in php

How to convert string to hexadecimal in php

藏色散人
Release: 2023-03-08 11:34:01
Original
3053 people have browsed it

php method to convert a string to hexadecimal: first create a PHP sample file; then create a strToHex method; then in the method body, use for loops and dechex and other functions to convert the string to hexadecimal function; finally convert it through the strToHex method.

How to convert string to hexadecimal in php

#The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.

PHP string and hexadecimal conversion

I am working on a project today, because I need to call the interface of other people’s websites, and the result needs to be correct in the request and return time. To perform hexadecimal encryption processing, I checked the information on the Internet and created a conversion demo for record keeping.

If used under TP, you can put the following functions in common.php

1. Encryption function

<?php
/**
*字符串转十六进制函数
*@pream string $str=&#39;abc&#39;;
*/
function strToHex($str){ 
$hex="";
for($i=0;$i<strlen($str);$i++)
$hex.=dechex(ord($str[$i]));
$hex=strtoupper($hex);
return $hex;
} 
?>
Copy after login

2. Decryption function

<?php
/**
*十六进制转字符串函数
*@pream string $hex=&#39;616263&#39;;
*/ 
function hexToStr($hex){   
$str=""; 
for($i=0;$i<strlen($hex)-1;$i+=2)
$str.=chr(hexdec($hex[$i].$hex[$i+1]));
return  $str;
} 
?>
Copy after login

【 Recommended study: "PHP Video Tutorial"]

The encryption and decryption conversion function uses a Demo example, which is written in a class for convenience.

<?php
class Test{ 
/**
*字符串转十六进制函数
*@pream string $str=&#39;abc&#39;;
*/
public function strToHex($str){ 
$hex="";
for($i=0;$i<strlen($str);$i++)
$hex.=dechex(ord($str[$i]));
$hex=strtoupper($hex);
return $hex;
}   
 
/**
*十六进制转字符串函数
*@pream string $hex=&#39;616263&#39;;
*/ 
public function hexToStr($hex){   
$str=""; 
for($i=0;$i<strlen($hex)-1;$i+=2)
$str.=chr(hexdec($hex[$i].$hex[$i+1]));
return  $str;
} 
}
 <span style="white-space:pre"></span>//测试Demo效果
$test = new Test();
$str = &#39;要加密的内容sxfenglei&#39;;
$data = $test->strToHex($str); 
echo &#39;加密内容:要加密的内容sxfenglei <br>&#39;.$data.&#39;<hr>&#39;;  
 
$output = $test->hexToStr($data);
echo &#39;解密内容:E8A681E58AA0E5AF86E79A84E58685E5AEB9737866656E676C6569 <br>&#39;.$output;  
?>
Copy after login

Run result:

加密内容:要加密的内容sxfenglei 
E8A681E58AA0E5AF86E79A84E58685E5AEB9737866656E676C6569
解密内容:E8A681E58AA0E5AF86E79A84E58685E5AEB9737866656E676C6569 
要加密的内容sxfenglei
Copy after login

The above is the detailed content of How to convert string to hexadecimal in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template