decimaal-naar-binair-2
Gesponsorde koppelingen
PHP script bestanden
Code (php)
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
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
<?php
function decbin2($number, $bits, $cutoff) {
$byte = "";
if($cutoff == "") {
$cutoff = false;
}
if(!$cutoff) {
$disp = true;
} else {
$disp = false;
}
while($bits > 0) {
$maxnr = pow(2,$bits-1);
if(($number-$maxnr) >= 0) {
$number = $number-$maxnr;
$byte .= "1";
$disp = true;
} else {
if($disp) {
$byte .= "0";
}
}
$bits--;
}
return $byte;
}
//voorbeeld:
$nr = 6000000001;
echo 'getal: '.$nr.'<hr>';
echo 'oude: '.decbin($nr).'<hr>';
echo 'nieuwe: '.decbin2($nr,256, true);//256 bits, met onnodige 0'en verwijderen
?>
function decbin2($number, $bits, $cutoff) {
$byte = "";
if($cutoff == "") {
$cutoff = false;
}
if(!$cutoff) {
$disp = true;
} else {
$disp = false;
}
while($bits > 0) {
$maxnr = pow(2,$bits-1);
if(($number-$maxnr) >= 0) {
$number = $number-$maxnr;
$byte .= "1";
$disp = true;
} else {
if($disp) {
$byte .= "0";
}
}
$bits--;
}
return $byte;
}
//voorbeeld:
$nr = 6000000001;
echo 'getal: '.$nr.'<hr>';
echo 'oude: '.decbin($nr).'<hr>';
echo 'nieuwe: '.decbin2($nr,256, true);//256 bits, met onnodige 0'en verwijderen
?>