import { encodeVarInt } from "./encodeVarInt" ; export class Hex { public static hexAlphabet = [ "0" , "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "a" , "b" , "c" , "d" , "e" , "f" , ] ; public static bigToHex ( num , bytes , le = true ) { let result = "" ; const bytemask = 255n ; const hquartet = 0b11110000 ; const lquartet = 0b1111 ; while ( bytes ) { const byte = Number ( num & bytemask ) ; const hex = Hex . hexAlphabet [ ( byte & hquartet ) >> 4 ] + Hex . hexAlphabet [ byte & lquartet ] ; num >>= 8n ; bytes -= 1 ; if ( le ) result += hex ; else result = hex + result ; } return result ; } public static uint8 ( value ) { return Hex . bigToHex ( BigInt ( value ) , 1 , true ) ; } public static uint16LE ( value ) { return Hex . bigToHex ( BigInt ( value ) , 2 , true ) ; } public static uint32LE ( value ) { return Hex . bigToHex ( BigInt ( value ) , 4 , true ) ; } public static uint64LE ( value ) { return Hex . bigToHex ( BigInt ( value ) , 8 , true ) ; } public static varint ( value ) { return encodeVarInt ( value ) . toString ( "hex" ) ; } public static buffer ( buf ) { return buf . toString ( "hex" ) ; } }