-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecimal.php
More file actions
42 lines (32 loc) · 1.08 KB
/
Decimal.php
File metadata and controls
42 lines (32 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
declare(strict_types=1);
namespace TinyBlocks\Encoder\Internal;
use TinyBlocks\Encoder\Base62;
final readonly class Decimal
{
private function __construct(private string $value)
{
}
public static function fromBase62(string $number, string $alphabet): Decimal
{
$value = '0';
$length = strlen($number);
for ($index = 0; $index < $length; $index++) {
$digit = strpos($alphabet, $number[$index]);
$value = bcmul($value, (string)Base62::BASE62_RADIX);
$value = bcadd($value, (string)$digit);
}
return new Decimal(value: $value);
}
public function toHexadecimal(): string
{
$value = $this->value;
$hexadecimalValue = '';
while (bccomp($value, '0') > 0) {
$remainder = bcmod($value, Hexadecimal::HEXADECIMAL_RADIX);
$hexadecimalValue = sprintf('%s%s', Hexadecimal::HEXADECIMAL_ALPHABET[(int)$remainder], $hexadecimalValue);
$value = bcdiv($value, Hexadecimal::HEXADECIMAL_RADIX);
}
return $hexadecimalValue;
}
}