PHP - bin2hex() 函式



PHP 的 bin2hex() 函式用於將二進位制資料(即二進位制資料的字串)轉換為十六進位制表示。換句話說,它將 ASCII 字串轉換為十六進位制值字串。

“十六進位制”數是以 16 為基數的數值系統中的數。在這裡,每個數字都可以用 0 到 9 和 A 到 F 的符號表示。

此函式不會將表示二進位制數字的字串(例如“1010”或“1100”)轉換為十六進位制值。另見 pack()

語法

以下是 PHP bin2hex() 函式的語法:

bin2hex(string $str): string 

引數

此函式接受單個引數,如下所述:

  • str - 需要轉換為十六進位制的字串。

返回值

此函式返回提供的字串的十六進位制表示形式。

示例 1

以下是 PHP bin2hex() 函式的基本示例:

<?php
   $str = "Tutorialspoint";
   echo "The given string: $str";
   echo "\nHexadecimal representation: ";
   #using bin2hex() function
   var_dump(bin2hex($str));
?>

輸出

這將產生以下輸出:

The given string: Tutorialspoint
Hexadecimal representation: string(28) "5475746f7269616c73706f696e74"

示例 2

以下是 PHP bin2hex() 函式的另一個示例。我們使用此函式來檢索此字串“Hello World!”的十六進位制表示形式:

<?php
   $str = "A";
   echo "The given string: $str";
   echo "\nHexadecimal representation: ";
   #using bin2hex() function
   var_dump(bin2hex($str));
?>

輸出

以下是上述程式的輸出:

The given string: A
Hexadecimal representation: string(2) "41"
php_function_reference.htm
廣告