问题
I am doing setbit operation in redis to mark which users have been online on a particular day.
I am doing a redis get operation to get the value of the key.
coffee> redis.setbit "a",7,1
true
coffee> redis.setbit "d",4,1
true
coffee> redis.setbit "g",1,1
true
coffee> redis.setbit "h",0,1
And the output is
coffee> redis.get "a",(err,res)->console.log res.toString().charCodeAt(0)
true
coffee> 1
coffee> redis.get "d",(err,res)->console.log res.toString().charCodeAt(0)
true
coffee> 8
coffee> redis.get "g",(err,res)->console.log res.toString().charCodeAt(0)
true
coffee> 64
coffee> redis.get "h",(err,res)->console.log res.toString().charCodeAt(0)
true
coffee> 65533
My problem is with the "h" key where I am setting the 0th bit 1. It should return 128 but returns 65533.Why is this so?
My end goal is to get bitmap from redis in binary so that I can get which users were active on that particular day.
回答1:
This error occurs because of utf-8 encoding. When we set the 0th bit as 1, it does not follow utf-8 rules. Now when we try to get it we get the replacement Character
U+FFFD � replacement character used to replace an unknown or unrepresentable character
and when we do a charCodeAt on it we will get 65533.
Read here UTF-8 and Specials Unicode Block
来源:https://stackoverflow.com/questions/32885532/get-operation-on-redis-bitmap-returns-weird-output