我在redis中做setbit操作,标记某天哪些用户在线。
我正在做一个redis get操作来获取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
输出是
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
我的问题是我将第 0 位设置为 1 的“h”键。它应该返回 128 但返回 65533。为什么会这样?
我的最终目标是从二进制的 redis 中获取位图,这样我就可以知道哪些用户在特定的一天是活跃的。
最佳答案
这个错误是因为utf-8编码造成的。当我们将第 0 位设置为 1 时,它不遵循 utf-8 规则。现在,当我们尝试获取它时,我们得到了替换字符
U+FFFD � replacement character used to replace an unknown or unrepresentable character
当我们对其执行 charCodeAt 时,我们将得到 65533。
阅读此处 UTF-8和 Specials Unicode Block
关于node.js - 对 Redis 位图的获取操作返回奇怪的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32885532/