I am using redis bitmap to record user action in a year, if user login in the first day in the year, I'll set the first bit of key to 1. the redis value like this:
key: user.18.action(18 is user id)
value: (bitmap) 000100000000000000000000...(about 365 bit long)
but when I was trying to get the value, GET user.18.action returns "\x10".
In PHP, how do I convert these strings to the 000100000000000000000000... thing?
Actually, I use the code below to implement it, but is this all right? and is there a better resolution?
$bitmap = $this->redis->get('user.18.action');
$binstrlen = strlen($bitmap);
$hex = bin2hex($bitmap);
$str = str_baseconvert($hex, 16, 2);
var_dump(str_pad($str, $binstrlen * 8, '0', STR_PAD_LEFT));
function str_baseconvert($str, $frombase=10, $tobase=36) {
$str = trim($str);
if (intval($frombase) != 10) {
$len = strlen($str);
$q = 0;
for ($i=0; $i<$len; $i++) {
$r = base_convert($str[$i], $frombase, 10);
$q = bcadd(bcmul($q, $frombase), $r);
}
}
else $q = $str;
if (intval($tobase) != 10) {
$s = '';
while (bccomp($q, '0', 0) > 0) {
$r = intval(bcmod($q, $tobase));
$s = base_convert($r, 10, $tobase) . $s;
$q = bcdiv($q, $tobase, 0);
}
}
else $s = $q;
return $s;
}
SETBIT.1?? @missingcat92