简体   繁体   中英

Python / Redis : Get Bitset string

I'm wondering how i can get the string value of a bitset in redis, i have the following code :

import redis as redis


def main():
    redisClient = redis.StrictRedis(host='localhost', port=6379, db=0)
    redisClient.setbit("mybitset",5,1)
    bitset=redisClient.get("mybitset")
    print bitset # expect the output to be 100000


if __name__=="__main__":
    main()

If I understand correctly the redisClient.get("mybitset") call returns a string containing the binary data. Try replacing your print bitset with:

print "{0:b}".format(ord(bitset[0]))

This will only work if your bitset is one byte long. If it spans multiple bytes try using the struct module. For example, if it is two bytes long (ie a short):

print "{0:b}".format(struct.unpack(">h", bitset)[0])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM