You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
950 B
40 lines
950 B
<?php
|
|
namespace App\Http\Services;
|
|
|
|
|
|
class CommonService{
|
|
/**
|
|
* 十进制小数转十六进制
|
|
* @param float $decimal
|
|
* @param $len
|
|
* @return string
|
|
*/
|
|
public static function customDec2hex(float $decimal, $len = 8) {
|
|
$hex = '';
|
|
$count = 0;
|
|
# 只取小数部分
|
|
$decimal = fmod($decimal, 1);
|
|
while ($count++ < $len) {
|
|
$dec16 = $decimal * 16;
|
|
# 取整 转16进制
|
|
$hex .= dechex((int)$dec16);
|
|
# 取小数部分
|
|
$decimal = fmod($dec16, 1);
|
|
}
|
|
return $hex;
|
|
}
|
|
|
|
/**
|
|
* 十六进制小数转十进制
|
|
* @param $hexadecimal
|
|
* @return float|int
|
|
*/
|
|
public static function customHex2dec($hexadecimal) {
|
|
$dec = 0;
|
|
for ($i = 0; $i < strlen($hexadecimal); $i++) {
|
|
$dec += hexdec($hexadecimal[$i]) / pow(16, $i + 1);
|
|
}
|
|
|
|
return $dec;
|
|
}
|
|
}
|
|
|