-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbcutil.php
More file actions
34 lines (28 loc) · 744 Bytes
/
bcutil.php
File metadata and controls
34 lines (28 loc) · 744 Bytes
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
<?php
function bc_to_binary($number, $bits = 0)
{
$bytes = array();
while ($number != "0")
{
$byte = (int)bcmod($number, "256");
array_unshift($bytes, chr($byte));
$number = bcdiv($number, "256");
}
$string = implode($bytes);
if (($width = $bits / 8) && strlen($string) < $width)
$string = str_pad($string, $width, "\0", STR_PAD_LEFT);
return $string;
}
function hex_to_bc($hex_number)
{
$s = 0;
if (substr($hex_number, 0, 2) == "0x")
$s = 2;
$value = "0";
for ($i = $s, $n = strlen($hex_number); $i < $n; $i += 1)
{
$value = bcmul($value, "16");
$value = bcadd($value, hexdec(substr($hex_number, $i, 1)));
}
return $value;
}