本文實例講述了PHP實現(xiàn)十進制數(shù)字與二十六進制字母串相互轉換操作。分享給大家供大家參考,具體如下:
?php /** * 將十進制數(shù)字轉換為二十六進制字母串 */ function num2alpha($intNum, $isLower=true) { $num26 = base_convert($intNum, 10, 26); $addcode = $isLower ? 49 : 17; $result = ''; for ($i=0; $istrlen($num26); $i++) { $code = ord($num26{$i}); if ($code 58) { $result .= chr($code+$addcode); } else { $result .= chr($code+$addcode-39); } } return $result; } /** * 將二十六進制字母串轉換為十進制數(shù)字 */ function alpha2num($strAlpha) { if (ord($strAlpha{0}) > 90) { $startCode = 97; $reduceCode = 10; } else { $startCode = 65; $reduceCode = -22; } $num26 = ''; for ($i=0; $istrlen($strAlpha); $i++) { $code = ord($strAlpha{$i}); if ($code $startCode+10) { $num26 .= $code-$startCode; } else { $num26 .= chr($code-$reduceCode); } } return (int)base_convert($num26, 26, 10); }
PS:這里再為大家推薦幾款計算與轉換工具供大家參考使用:
在線任意進制轉換工具:
http://tools.jb51.net/transcoding/hexconvert
科學計算器在線使用_高級計算器在線計算:
http://tools.jb51.net/jisuanqi/jsqkexue
在線計算器_標準計算器:
http://tools.jb51.net/jisuanqi/jsq
更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP數(shù)據(jù)結構與算法教程》、《php程序設計算法總結》、《php字符串(string)用法總結》、《PHP數(shù)組(Array)操作技巧大全》、《PHP常用遍歷算法與技巧總結》及《PHP數(shù)學運算技巧總結》
希望本文所述對大家PHP程序設計有所幫助。