(bitcoin-specific): convert an integer into a variable-length integer
422815
def num_to_var_int(x):
    
    x = int(x)
    if x < 253:
        return from_int_to_byte(x)

    elif x < 65536:
        return from_int_to_byte(253) + encode(x, 256, 2)[::-1]

    elif x < 4294967296:
        return from_int_to_byte(254) + encode(x, 256, 4)[::-1]

    else:
        return from_int_to_byte(255) + encode(x, 256, 8)[::-1]
176755
function uint32BEtoNumber(bytes) {
  let n = 0;
  for (let i = 0; i < 3; i++) {
    n |= bytes[i];
    n <<= 8;
  }
  n |= bytes[3];
  return n;
}
1284485
static void int2bin(
	 int value,             /* input : decimal value */
	 int no_of_bits,        /* input : number of bits to use */
	 short[] bitstream, int bitstreams       /* output: bitstream  */
	)
	{
	   int pt_bitstream;
	   short   i, bit;

	   pt_bitstream = bitstreams + no_of_bits;

	   for (i = 0; i < no_of_bits; i++)
	   {
	     bit = (short)(value & 0x0001);      /* get lsb */
	     if (bit == 0)
	         bitstream[--pt_bitstream] = LD8KConstants.BIT_0;
	     else
	         bitstream[--pt_bitstream] = LD8KConstants.BIT_1;
	     value >>= 1;
	   }
	}
689578
def b58encode(v):
    

    long_value = 0
    for c in v:
        long_value = long_value * 256 + c

    result = ""
    while long_value >= __b58base:
        div, mod = divmod(long_value, __b58base)
        result = __b58chars[mod] + result
        long_value = div
    result = __b58chars[long_value] + result

    # Bitcoin does a little leading-zero-compression:
    # leading 0-bytes in the input become leading-1s
    nPad = 0
    for c in v:
        if c == 0:
            nPad += 1
        else:
            break

    return (__b58chars[0] * nPad) + result
195333
def integer_byte_size(n):
    ''''''
    quanta, mod = divmod(integer_bit_size(n), 8)
    if mod or n == 0:
        quanta += 1
    return quanta
235262
def get_integers(bitwidth, unsigned, limit=0):
    '''
    
    '''
    if unsigned:
        start, stop = 0, ((1 << bitwidth) - 1)
    else:
        start, stop = (-(1 << bitwidth-1)), (1 << (bitwidth-1)-1)

    for num in _fuzzdb_integers(limit):
        if num >= start and num <= stop:
            yield num
953118
def to_int(value: str) -> int:
    
    if is_0x_prefixed(value):
        if len(value) == 2:
            return 0
        else:
            return int(value, 16)
    else:
        return int(value)
549872
def decode_int(v):
    """"""
    if len(v) > 0 and (v[0] == b'\x00' or v[0] == 0):
        raise Exception("No leading zero bytes allowed for integers")
    return big_endian_to_int(v)
944591
def bigint_to_string(val):
    
    if isinstance(val, _NUMBERS) and not abs(val) <= 2**53-1:
        return str(val)
    return val
669042
def to_binary(decimal, length):
        
        b = bitstring.Bits(int=decimal, length=length)
        return b.bin
900269
def int_to_bytes(i, minlen=1, order='big'):  # pragma: no cover
        """"""
        blen = max(minlen, PGPObject.int_byte_len(i), 1)

        if six.PY2:
            r = iter(_ * 8 for _ in (range(blen) if order == 'little' else range(blen - 1, -1, -1)))
            return bytes(bytearray((i >> c) & 0xff for c in r))

        return i.to_bytes(blen, order)
115277
def _convert_number(self, number):
        """"""

        number = float(number)
        return int(number) if number.is_integer() else float(number)
193864
function uint16BEtoNumber(bytes) {
  var n = 0;
  for (var i = 0; i < 1; i++) {
    n |= bytes[i];
    n <<= 8;
  }
  n |= bytes[1];
  return n;
}
989794
def from_bytes(VarInt, byte_string):
        '''
        
        '''
        num = byte_string
        if num[0] <= 0xfc:
            num = num[0:1]
            non_compact = False
        elif num[0] == 0xfd:
            num = num[1:3]
            non_compact = (num[-1:] == b'\x00')
        elif num[0] == 0xfe:
            num = num[1:5]
            non_compact = (num[-2:] == b'\x00\x00')
        elif num[0] == 0xff:
            num = num[1:9]
            non_compact = (num[-4:] == b'\x00\x00\x00\x00')
        if len(num) not in [1, 2, 4, 8]:
            raise ValueError('Malformed VarInt. Got: {}'
                             .format(byte_string.hex()))

        if (non_compact
            and ('overwinter' in riemann.get_current_network_name()
                 or 'sapling' in riemann.get_current_network_name())):
            raise ValueError('VarInt must be compact. Got: {}'
                             .format(byte_string.hex()))

        ret = VarInt(
            utils.le2i(num),
            length=len(num) + 1 if non_compact else 0)

        return ret
312998
def _int_size_to_type(size):
    
    if size <= 8:
        return ByteType
    if size <= 16:
        return ShortType
    if size <= 32:
        return IntegerType
    if size <= 64:
        return LongType
209301
def num2varint(num):
    
    # if (typeof num !== 'number') throw new Error('VarInt must be numeric')
    # if (num < 0) throw new RangeError('VarInts are unsigned (> 0)')
    # if (!Number.isSafeInteger(num)) throw new RangeError('VarInt must be a safe integer')
    if num < 0xfd:
        return num2hexstring(num)
    elif num <= 0xffff:
        # uint16
        return 'fd' + num2hexstring(number=num, size=2, little_endian=True)
    elif num <= 0xffffffff:
        # uint32
        return 'fe' + num2hexstring(number=num, size=4, little_endian=True)
    else:
        # uint64
        return 'ff' + num2hexstring(number=num, size=8, little_endian=True)
1921
function(length) {
      var array, byte, _i, _len, _results;
      array = new Uint8Array(length);
      window.crypto.getRandomValues(array);
      _results = [];
      for (_i = 0, _len = array.length; _i < _len; _i++) {
        byte = array[_i];
        _results.push(byte);
      }
      return _results;
    }