1

I want to store data in redis using bitfield. The string I get is "|\x00\x82" I tried converting ASCII characters to binary with this

for (let i = 0; i < map.length; i++) {
    let byte = map[i].charCodeAt(0).toString(2).padStart(8, "0");
    bString += byte;
  }

The result of this is 01111100 00000000 11111111 11111101 But there are only three characters in the string. the first two match and I don't know what are the other two. It doesn't even change when i try to store something else yet in redis-cli it changes. I noticed that this happens when the most significant bit is 1. I tried it with unsigned and signed integer.

1 Answer 1

4

If you want to read and write binary data in Redis, you can uses buffers with Node Redis:

await redis.set('foo', Buffer.from([ 0, 1, 2, 3, 4, 5 ]))
let result = await redis.get(commandOptions({ returnBuffers: true }), 'foo')

console.log(result) // <Buffer 00 01 02 03 04 05>
Sign up to request clarification or add additional context in comments.

1 Comment

charCodeAt() was the problem. When was the first bit 1 the return was 2 byte insted of one

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.