Converts an ip to a hex value that can be used with a hex bit mask
348608
def ip2hex(ip):
    '''
    
    '''
    parts = ip.split(".")
    if len(parts) != 4: return None
    ipv = 0
    for part in parts:
        try:
            p = int(part)
            if p < 0 or p > 255: return None
            ipv = (ipv << 8) + p
        except:
            return None
    return ipv
301676
def i2osp(self, long_integer, block_size):
        ''
        hex_string = '%X' % long_integer
        if len(hex_string) > 2 * block_size:
            raise ValueError('integer %i too large to encode in %i octets' % (long_integer, block_size))
        return a2b_hex(hex_string.zfill(2 * block_size))
158344
def hex(self):
        """"""
        addrstr = '000000'
        if self.addr is not None:
            addrstr = binascii.hexlify(self.addr).decode()
        return addrstr
1067836
def to_hex(
    primitive: Primitives = None, hexstr: HexStr = None, text: str = None
) -> HexStr:
    
    if hexstr is not None:
        return HexStr(add_0x_prefix(hexstr.lower()))

    if text is not None:
        return HexStr(encode_hex(text.encode("utf-8")))

    if is_boolean(primitive):
        return HexStr("0x1") if primitive else HexStr("0x0")

    if isinstance(primitive, (bytes, bytearray)):
        return HexStr(encode_hex(primitive))
    elif is_string(primitive):
        raise TypeError(
            "Unsupported type: The primitive argument must be one of: bytes,"
            "bytearray, int or bool and not str"
        )

    if is_integer(primitive):
        return HexStr(hex(cast(int, primitive)))

    raise TypeError(
        "Unsupported type: '{0}'.  Must be one of: bool, str, bytes, bytearray"
        "or int.".format(repr(type(primitive)))
    )
922377
def convert(self, value, param, ctx):
        
        rxp = "^(((([1]?\d)?\d|2[0-4]\d|25[0-5])\.){3}(([1]?\d)?\d|2[0-4]\d|"\
              "25[0-5]))|([\da-fA-F]{1,4}(\:[\da-fA-F]{1,4}){7})|(([\da-fA-F]"\
              "{1,4}:){0,5}::([\da-fA-F]{1,4}:)"\
              "{0,5}[\da-fA-F]{1,4})$"
        regex = re.compile(rxp, re.I)
        backend = {}
        if value.count(':') == 0:
            # port is not set
            backend['ip'] = value
        elif value.count(':') == 7:
            # it's an ipv6 without port
            backend['ip'] = value
        elif value.count(':') == 8:
            # it's an ipv6 with port
            backend['ip'] = value.rsplit(':', 1)[0]
            backend['port'] = int(value.rsplit(':', 1)[1])
        else:
            backend['ip'] = value.split(':')[0]
            backend['port'] = int(value.split(':')[1])
        try:
            if regex.match(backend['ip']):
                return backend
            else:
                self.fail('%s is not a valid ip address' %
                          backend['ip'], param, ctx)
        except ValueError:
            self.fail('%s is not a valid ip address' % backend['ip'], param,
                      ctx)
910214
def __ipv4_netmask(value):
    ''''''
    valid, errmsg = False, 'dotted quad or integer CIDR (0->32)'
    valid, value, _ = __int(value)
    if not (valid and 0 <= value <= 32):
        valid = salt.utils.validate.net.netmask(value)
    return (valid, value, errmsg)
867556
def _idx_to_bits(self, i):
    """"""
    bits = bin(i)[2:].zfill(self.nb_hyperplanes)  # Pad the bits str with 0
    return [-1.0 if b == "0" else 1.0 for b in bits]
525311
def to_i
      self.address.split(':').inject(0) do |bits,char|
        bits = ((bits << 8) | char.hex)
      end
    end
386600
def long_to_hex(l, size):
    """"""
    f_str = "{0:0%sx}" % size
    return ensure_bytes(f_str.format(l).lower())
1293802
def combine_hex(data):
    '''  '''
    output = 0x00
    for i, value in enumerate(reversed(data)):
        output |= (value << i * 8)
    return output
1080115
def format_hex(i, num_bytes=4, prefix='0x'):
    
    prefix = str(prefix or '')
    i = int(i or 0)
    return prefix + '{0:0{1}x}'.format(i, num_bytes)
83540
def _hex_to_octets(addr):
    '''
    
    '''
    return '{0}:{1}:{2}:{3}'.format(
        int(addr[6:8], 16),
        int(addr[4:6], 16),
        int(addr[2:4], 16),
        int(addr[0:2], 16),
    )
993353
def cidr_to_ipv4_netmask(cidr_bits):
    '''
    
    '''
    try:
        cidr_bits = int(cidr_bits)
        if not 1 <= cidr_bits <= 32:
            return ''
    except ValueError:
        return ''

    netmask = ''
    for idx in range(4):
        if idx:
            netmask += '.'
        if cidr_bits >= 8:
            netmask += '255'
            cidr_bits -= 8
        else:
            netmask += '{0:d}'.format(256 - (2 ** (8 - cidr_bits)))
            cidr_bits = 0
    return netmask
1340123
def ip_to_bytes(ip_str, big_endian=True):
    
    if big_endian:
        code = '>L'
    else:
        code = '<L'
    return bytes(struct.unpack(code, socket.inet_aton(ip_str))[0])
91869
def hex_to_rgb(hex_value):
    
    if '#' in hex_value:
        hex_value = hex_value.replace('#', '')
    if len(hex_value) < 6:
        return '0000/0000/0000'
    r = hex_value[:2]
    g = hex_value[2:4]
    b = hex_value[4:]
    return r + '00/' + g + '00/' + b + '00'
880687
def from_hex(cls, value):
        """"""
        if len(value) == 8:
            return cls(int(value, 16))
        elif len(value) == 32:
            return cls(int(value, 16))
        else:
            raise ValueError('%r: invalid hexadecimal notation' % (value,))
48138
def pad_hex(value, bit_size):
    
    value = remove_0x_prefix(value)
    return add_0x_prefix(value.zfill(int(bit_size / 4)))