Home > Backend Development > PHP Tutorial > What are the functions for converting php to hexadecimal?

What are the functions for converting php to hexadecimal?

青灯夜游
Release: 2023-04-10 20:42:01
Original
5090 people have browsed it

php functions to convert to hexadecimal include: 1. bin2hex(), which can convert a string of ASCII characters into a hexadecimal value; 2. dechex(), which can convert a decimal number into a hexadecimal value. Base number, 3, base_convert(), can convert numbers between any bases.

What are the functions for converting php to hexadecimal?

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

php to 16 System function

1, bin2hex() function

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

Syntax: bin2hex(string)

Return value: Returns the hexadecimal value of the string to be converted.

Example:

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

What are the functions for converting php to hexadecimal?

2. dechex() function

dechex() function converts decimal numbers is a hexadecimal number.

Syntax: dechex(number);

Return value: a string containing a hexadecimal number with a decimal value.

Example:

<?php
echo dechex("30") . "<br>";
echo dechex("10") . "<br>";
echo dechex("1587") . "<br>";
echo dechex("70");
?>
Copy after login

What are the functions for converting php to hexadecimal?

Description:

hexdec() Converts a hexadecimal string to a decimal number. The maximum value that can be converted is 7ffffffff, which is 2147483647 in decimal. Starting with PHP 4.1.0, this function can handle large numbers, in which case it returns a float type.

hexdec() Replaces all non-hexadecimal characters encountered with 0. This way, all zeros on the left are ignored, but zeros on the right are included in the value.

3. base_convert() function

base_convert() function converts numbers between arbitrary bases.

Syntax:base_convert(number,frombase,tobase);

ParametersDescription
numberRequired. Specifies the number to be converted.
frombaseRequired. Specifies the original base of the number. Between 2 and 36 (inclusive). Numbers above decimal are represented by the letters a-z, such as a for 10, b for 11, and z for 35.
tobaseRequired. Specifies the base to be converted. Between 2 and 36 (inclusive). Numbers above decimal are represented by the letters a-z, such as a for 10, b for 11, and z for 35.

When the value of parameter tobase is 16, other base numbers can be converted into hexadecimal numbers.

Example:

<?php
echo base_convert("30", 10, 16) . "<br>";
echo base_convert("364", 8, 16) . "<br>";
?>
Copy after login

What are the functions for converting php to hexadecimal?

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What are the functions for converting php to hexadecimal?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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