Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert redis bitmap(binary string) to binary number with leading zero?

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; 
}
like image 310
missingcat92 Avatar asked Apr 08 '26 21:04

missingcat92


1 Answers

function bitmap_human($bitmap){
    $bytes = unpack('C*', $bitmap);
    $bin = join(array_map(function($byte){
        return sprintf("%08b", $byte);
    }, $bytes));
    return $bin;
}

I figured this out, works all right.

like image 62
missingcat92 Avatar answered Apr 10 '26 13:04

missingcat92



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!