ProTheJoker ProTheJoker - 3 months ago 16
Javascript Question

How can % 8 actually return 8?

I have this function for custom padding, which works like this:

var length = 8 - ( (this.buffer.length - 1 + this.buffer.length - 39) % 8);
var padding = Buffer.alloc(length + 1);
for (i = 1; i <= length; i++) {
padding.writeUInt8(i, i-1);
}
padding.writeUInt8(length, length);
this.buffer = Buffer.concat([this.buffer, padding]);


where this.buffer may have variable length. (always > 0)
However, how can there be a number which lets length be 8?
Since I'm doing 8 - x % 8, I'm expecting values between 0 and 7, or am I not?

Answer

x %8 varies from 0 to 7.

8 - x%8 varies from 8-0 == 8 to 8-7 == 1.