Home  >  Article  >  Backend Development  >  How to convert string to ASCII code using PHP

How to convert string to ASCII code using PHP

PHPz
PHPzOriginal
2023-04-10 14:12:582111browse

PHP is a very popular programming language used for developing server-side applications. In PHP, string is a very common data type. In some cases, we need to convert the string to ASCII code. This article will introduce how to use PHP to convert strings to ASCII codes.

1. What is ASCII code?

ASCII code is an encoding system used to represent characters. It uses 7-bit or 8-bit binary numbers to represent 128 or 256 different characters. In the ASCII code table, each character has a corresponding numeric value. For example, the ASCII code value of the letter "A" is 65, and the ASCII code value of the letter "B" is 66.

In computer science, ASCII codes are commonly used to store and transmit text data. In PHP, we can use built-in functions to convert strings to ASCII codes.

2. Use PHP to convert the string into ASCII code

  1. Use the ord() function

The ord() function is a built-in in PHP Function used to convert a single character in a string to its ASCII code value. The following is a code example that uses the ord() function to convert a string into ASCII code:

$str = "Hello, World!";
for ($i = 0; $i < strlen($str); $i++) {
    echo ord($str[$i]) . " ";
}

The output result is:

72 101 108 108 111 44 32 87 111 114 108 100 33

The above code converts each character in the string "Hello, World!" characters into their corresponding ASCII code values ​​and print them to the screen.

  1. Use the unpack() function

The unpack() function can convert a binary string into an array, where each element is a byte in the string . Therefore, we can use the unpack() function to convert a string to its ASCII code value.

The following is a code example that uses the unpack() function to convert a string into ASCII code:

$str = "Hello, World!";
$bytes = unpack('C*', $str);
foreach ($bytes as $byte) {
    echo $byte . " ";
}

The output result is:

72 101 108 108 111 44 32 87 111 114 108 100 33

In the above code, the unpack() function Format the string using 'C', where 'C' means unsigned characters and '' means unpacking the entire string. After this, we loop through the resulting array and print each element to the screen.

3. Summary

In PHP, converting string to ASCII code is a very common task. We can use the ord() function or unpack() function to complete this task. The ord() function converts a single character in a string to its corresponding ASCII code value, while the unpack() function converts the entire string into a byte array, where each element is a byte in the string. Hope this article can help you learn how to do string conversion in PHP.

The above is the detailed content of How to convert string to ASCII code using PHP. 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