2

I am programming a Node JS client that's querying a third-party server for information. Part of the message that I receive back from the third party server is a UINT 128 (specified in documentation). However, from what I can tell, NodeJS buffers do not support reading 128 bit ints from buffers. I have checked this page and the two most likely functions (reading BIGINTS and reading unspecified byte length ints) both don't support 128 bit integers.

I've already tried installing and using the big-integer module, but if that is the correct method, I couldn't understand how to read the bytes out of the buffer into a big-integer object.

How can I read a UINT128 out of a NodeJS buffer? I'm' willing to use bit arithmetic if necessary, I'm just not sure how.

greyvest
  • 23
  • 2

1 Answers1

0

I did it by using getBigUint64 on a DataView twice, then calling toString on both, concatenating the strings and parsing that string as a BigInt... it's dumb but it seems to work. You may also be able to concat the 64bit BigInts using bitwise operators but I haven't tried.

readUInt128() {
    let a = this.readUInt64();
    let b = this.readUInt64();
    return BigInt(a.toString() + b.toString());
}

readUInt64() {
    let result = this.view.getBigUint64(this.pos, this.littleEndian)
    this.pos += 8
    return result
}
RuteNL
  • 197
  • 14