How to implement the conversion function between hexadecimal and decimal in php

墨辰丷
Release: 2023-03-28 08:44:01
Original
1717 people have browsed it

This article mainly introduces the function of converting hexadecimal to decimal in PHP, and analyzes related techniques of PHP numerical operation and string operation in the form of examples. Friends in need can refer to this article

The example describes how PHP implements the conversion function between hexadecimal and decimal. Share it with everyone for your reference, the details are as follows:

/**
 * @desc im:十进制数转换成三十六机制数
 * @param (int)$num 十进制数
 * return 返回:三十六进制数
*/
function get_char($num) {
  $num = intval($num);
  if ($num <= 0)
    return false;
  $charArr = array("0","1","2","3","4","5","6","7","8","9",&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;, &#39;G&#39;, &#39;H&#39;, &#39;I&#39;, &#39;J&#39;, &#39;K&#39;, &#39;L&#39;, &#39;M&#39;, &#39;N&#39;, &#39;O&#39;, &#39;P&#39;, &#39;Q&#39;, &#39;R&#39;, &#39;S&#39;, &#39;T&#39;, &#39;U&#39;, &#39;V&#39;, &#39;W&#39;, &#39;X&#39;, &#39;Y&#39;, &#39;Z&#39;);
  $char = &#39;&#39;;
  do {
    $key = ($num - 1) % 36;
    $char= $charArr[$key] . $char;
    $num = floor(($num - $key) / 36);
  } while ($num > 0);
  return $char;
}
/**
 * @desc im:三十六进制数转换成十机制数
 * @param (string)$char 三十六进制数
 * return 返回:十进制数
 */
function get_num($char){
  $array=array("0","1","2","3","4","5","6","7","8","9","A", "B", "C", "D","E", "F", "G", "H", "I", "J", "K", "L","M", "N", "O","P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y","Z");
  $len=strlen($char);
  for($i=0;$i<$len;$i++){
    $index=array_search($char[$i],$array);
    $sum+=($index+1)*pow(36,$len-$i-1);
  }
  return $sum;
}
Copy after login

Usage examples:

echo "get_char:".get_char(514549)."<br>";
echo "get_num:".get_num(&#39;A0ZZ&#39;)."<br>";
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone’s study.


Related recommendations:

php method to implement xmlconversionarray

How to implement Vue to transmit the html field string in the backgroundConvert to HTML

PHP systemConversionExample Detailed explanation

The above is the detailed content of How to implement the conversion function between hexadecimal and decimal in php. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!