Convert ASCII characters to hexadecimal values ​​in minutes

醉折花枝作酒筹
Release: 2023-03-11 21:20:01
Original
10376 people have browsed it

In the previous article, we learned what string functions are and how to insert a backslash before a character. If you need it, please read "Teach you how to use string functions to insert a backslash before a character. Gang》. This time we will introduce to you the method of converting ASCII character strings and hexadecimal values. You can refer to it if you need it.

Before we introduce how to convert them to each other, let's first take a look at what ASCII characters are? What is a hexadecimal value?

ASCII (American Standard Code for Information Interchange) is a computer coding system based on the Latin alphabet, primarily used to display modern English and other Western European languages. It is the most common information exchange standard and is equivalent to the international standard ISO/IEC 646. ASCII code uses a specified combination of 7- or 8-bit binary numbers to represent 128 or 256 possible characters.

Hexadecimal (abbreviated as hex or subscript 16) is the carry system in mathematics. Usually, it is represented by the numbers 0 to 9 and the letters a to f (or a~f), where a~f represents 10 to 15, which is called a hexadecimal number.

Now that we have the basics, let’s take a look at their conversions. First let's look at converting ASCII characters to hexadecimal values.

First let’s look at a small example.

<?php
$str = bin2hex("Hello World!");
echo($str);
?>
Copy after login

The result is

48656c6c6f20576f726c6421
Copy after login

As you can see from this example, if we want to convert ASCII characters to hexadecimal values, we can use the bin2hex function.

Let’s take a look at this function.

bin2hex() function converts a string of ASCII characters into a hexadecimal value.

Let’s take a look at its grammatical format to avoid making mistakes.

bin2hex(要转换的字符串)
Copy after login

Now that we have seen how to convert ASCII characters to hexadecimal values, let’s take a look at how to convert hexadecimal values ​​to ASCII characters.

First let’s look at a little chestnut.

<?php
$str = &#39;49206c6f7665e4b8ade59bbd&#39;;
$str = hex2bin($str);
echo $str;
?>
Copy after login

The result is

I love中国
Copy after login

Let’s take a look at the example, which converts the numerical value into letters and Chinese, so that we know that if we want to convert the hexadecimal value into ASCII characters Use the hex2bin function.

Let’s take a look at this function in detail.

hex2bin() function converts a string of hexadecimal values ​​into ASCII characters.

Let’s take a look at its grammatical format to avoid making mistakes.

hex2bin(要转换的十六进制值)
Copy after login

That’s all. If you want to know anything else, you can click this. → →php video tutorial

The above is the detailed content of Convert ASCII characters to hexadecimal values ​​in minutes. 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