I try to create integers from raw-data that I read from '/dev/urandom'. But I do not know how to convert to an integer.
<?php
//////////////////////////////////////////////////////////////////////////////
function base62_encode($val, $base=62, $chars='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
if(!isset($base))
$base = strlen($chars);
$str = '';
do {
$m = bcmod($val, $base);
$str = $chars[$m] . $str;
$val = bcdiv(bcsub($val, $m), $base);
} while(bccomp($val,0)>0);
return $str;
}
//////////////////////////////////////////////////////////////////////////////
$file = fopen('/dev/urandom', 'rb');
if ( !$file ) {
throw new Exception('Error!');
} else {
$binary = fread($file, 4); // 4 bytes == 32 bits
fclose($file);
$dec = (int)$binary;
$base62 = base62_encode($dec);
fprintf(STDOUT, "dec:%d, %s\n", $dec, $base62);
}
//////////////////////////////////////////////////////////////////////////////
?>
As output I get: "dec:0, 0"
You can test code here.