Home Backend Development PHP Problem How to convert string to ASCII code using PHP

How to convert string to ASCII code using PHP

Apr 10, 2023 pm 02:12 PM

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]) . " ";
}
Copy after login

The output result is:

72 101 108 108 111 44 32 87 111 114 108 100 33
Copy after login
Copy after login

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 . " ";
}
Copy after login

The output result is:

72 101 108 108 111 44 32 87 111 114 108 100 33
Copy after login
Copy after login

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 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

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)