encode_NUMBER function

dynamic encode_NUMBER(
  1. dynamic v
)

Convert a numeric operand or charstring number to a variable-size list of bytes. @param {number} @returns {Array}

Implementation

encode_NUMBER(v) {
    if (v >= -107 && v <= 107) {
        return [v + 139];
    } else if (v >= 108 && v <= 1131) {
        v = v - 108;
        return [(v >> 8) + 247, v & 0xFF];
    } else if (v >= -1131 && v <= -108) {
        v = -v - 108;
        return [(v >> 8) + 251, v & 0xFF];
    } else if (v >= -32768 && v <= 32767) {
        return encode_NUMBER16(v);
    } else {
        return encode_NUMBER32(v);
    }
}