search
HomeBackend DevelopmentPHP ProblemHow to convert php string to hexadecimal

How to convert php string to hexadecimal: first create a PHP sample file; then use the "function strToHex($str) {...}" method to convert the string to hexadecimal .

How to convert php string to hexadecimal

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

PHP string and hexadecimal How to achieve mutual conversion

I was working on a project today. Because I needed to call the interface of other people’s websites, the result required hexadecimal encryption of the request and return time, so I checked the information on the Internet. Thanks for 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;
}
?>

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;
}
?>

The encryption and decryption conversion function uses the Demo example, which is written in a class for convenience. [Recommended learning: "PHP Video Tutorial"]

<?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;
  }
} < spanstyle = "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;
?>

Running results:

Encrypted content: Content to be encrypted sxfenglei
E8A681E58AA0E5AF86E79A84E58685E5AEB9737866656E676C6569
Decrypted content:E8A681E58AA0E5AF86E79A84E58685E5AEB9737866656E676C6569
Content to be encrypted sxfenglei

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

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software