我正在做一个redis get操作,以获取键。
咖啡> redis.setbita,7,1 true coffee> redis.setbitd,4,1 true coffee> redis.setbitg,1,1 true coffee> redis.setbith,0,1输出
咖啡> redis.geta,(err,res) - > console.log res.toString()。charCodeAt(0) true coffee> 1 咖啡> redis.getd,(err,res) - > console.log res.toString()。charCodeAt(0) true coffee> 8 coffee> redis.getg,(err,res) - > console.log res.toString()。charCodeAt(0) true coffee> 64 咖啡> redis.geth,(err,res) - > console.log res.toString()。charCodeAt(0) true coffee> 65533我的问题是用h键设置第0位。返回128但返回65533.为什么是这样的?
我的最终目标是从二进制的redis获取位图,以便我可以获得哪些用户在那一天活动。
解决方案由于utf-8编码,发生此错误。当我们将第0位设置为1时,它不遵循utf-8规则。现在,当我们尝试获取它,我们得到替换字符
U + FFFD 替换字符用于替换未知或无法表示的字符
,当我们对它进行charCodeAt时,我们将获得65533.
请在这里阅读 UTF-8 和 Specials Unicode Block
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,1And 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> 65533My 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.
解决方案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
更多推荐
Get操作对Redis位图返回奇怪的输出
发布评论