35 lines
683 B
JavaScript
35 lines
683 B
JavaScript
'use strict'
|
|
const varint = require('varint')
|
|
|
|
module.exports = {
|
|
numberToBuffer,
|
|
bufferToNumber,
|
|
varintBufferEncode,
|
|
varintBufferDecode,
|
|
varintEncode
|
|
}
|
|
|
|
function bufferToNumber (buf) {
|
|
return parseInt(buf.toString('hex'), 16)
|
|
}
|
|
|
|
function numberToBuffer (num) {
|
|
let hexString = num.toString(16)
|
|
if (hexString.length % 2 === 1) {
|
|
hexString = '0' + hexString
|
|
}
|
|
return Buffer.from(hexString, 'hex')
|
|
}
|
|
|
|
function varintBufferEncode (input) {
|
|
return Buffer.from(varint.encode(bufferToNumber(input)))
|
|
}
|
|
|
|
function varintBufferDecode (input) {
|
|
return numberToBuffer(varint.decode(input))
|
|
}
|
|
|
|
function varintEncode (num) {
|
|
return Buffer.from(varint.encode(num))
|
|
}
|