3

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()

1 Answer 1

4

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 (i.e. a short):

print "{0:b}".format(struct.unpack(">h", bitset)[0])
Sign up to request clarification or add additional context in comments.

1 Comment

Doesn't work : using the first print i get output as 100, using the second option print "{0:b}".format(struct.unpack(">h", bitset)[0]) I get error struct.error: unpack requires a string argument of length 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.