export class Stack { param buf public static decodeNum ( buf ) { let result = 0n ; if ( buf . length === 0 ) { return result ; } const be = Buffer . from ( buf ) . reverse ( ) ; let neg = false ; if ( be [ 0 ] & 0x80 ) { neg = true ; be [ 0 ] = be [ 0 ] & 0x7f ; } result = BigInt ( be [ 0 ] ) ; for ( let i = 1 ; i < be . length ; i ++ ) { result <<= 8n ; result += BigInt ( be [ i ] ) ; } if ( neg ) { return - result ; } else { return result ; } } param num public static encodeNum ( input ) { const num = BigInt ( input ) ; const bytes = [ ] ; const neg = num < 0 ; let abs = num > 0 ? num : - num ; while ( abs > 0 ) { bytes . push ( Number ( abs & BigInt ( 0xff ) ) ) ; abs >>= 8n ; } if ( bytes [ bytes . length - 1 ] & 0x80 ) { if ( neg ) { bytes . push ( 0x80 ) ; } else { bytes . push ( 0x00 ) ; } } else { if ( neg ) { bytes [ bytes . length - 1 ] |= 0x80 ; } } return Buffer . from ( bytes ) ; } }