Example #1
0
def to_text(primitive=None, hexstr=None, text=None):
    assert_one_val(primitive, hexstr=hexstr, text=text)

    if hexstr is not None:
        return to_bytes(hexstr=hexstr).decode('utf-8')
    elif text is not None:
        return text
    elif isinstance(primitive, str):
        return to_text(hexstr=primitive)
    elif isinstance(primitive, bytes):
        return primitive.decode('utf-8')
    elif is_integer(primitive):
        byte_encoding = int_to_big_endian(primitive)
        return to_text(byte_encoding)
    raise TypeError("Expected an int, bytes or hexstr.")
Example #2
0
def to_bytes(primitive=None, hexstr=None, text=None):
    assert_one_val(primitive, hexstr=hexstr, text=text)

    if is_boolean(primitive):
        return b'\x01' if primitive else b'\x00'
    elif isinstance(primitive, bytes):
        return primitive
    elif is_integer(primitive):
        return to_bytes(hexstr=to_hex(primitive))
    elif hexstr is not None:
        if len(hexstr) % 2:
            hexstr = '0x0' + remove_0x_prefix(hexstr)
        return decode_hex(hexstr)
    elif text is not None:
        return text.encode('utf-8')
    raise TypeError("expected an int in first arg, or keyword of hexstr or text")
Example #3
0
def to_boolean(value=None, intval=None, hexstr=None, text=None):
    """
    Converts value to it's boolean representation.

    Values are converted this way:

     * value:
       * bytes: big-endian integer
       * big_endian_integer: 1=>True, 0=>False
     * hexstr: interpret hex as integer
     * text: interpret as string of digits, like '1' => True
    """
    assert_one_val(value, intval=intval, hexstr=hexstr, text=text)

    if intval is not None:
        if intval == 0:
            return (False)
        elif intval == 1:
            return (True)
        else:
            raise TypeError("Cannot convert int to boolean")
    elif hexstr is not None:
        if hexstr == '0x0':
            return (False)
        elif hexstr == '0x01':
            return (True)
        else:
            raise TypeError("Cannot convert hex number to boolean")
    elif text is not None:
        if text == '0':
            return (False)
        elif text == '1':
            return (True)
        else:
            raise TypeError("Cannot convert text string to boolean")
    elif isinstance(value, bytes):
        if value == b'0':
            return (False)
        elif value == b'1':
            return (True)
        else:
            raise TypeError("Cannot convert byte to boolean")
    elif isinstance(value, str):
        raise TypeError("Pass in strings with keyword hexstr or text")
    else:
        return bool(value)
Example #4
0
def to_int(value=None, hexstr=None, text=None):
    """
    Converts value to it's integer representation.

    Values are converted this way:

     * value:
       * bytes: big-endian integer
       * bool: True => 1, False => 0
     * hexstr: interpret hex as integer
     * text: interpret as string of digits, like '12' => 12
    """
    assert_one_val(value, hexstr=hexstr, text=text)

    if hexstr is not None:
        return int(hexstr, 16)
    elif text is not None:
        return int(text)
    elif isinstance(value, bytes):
        return big_endian_to_int(value)
    elif isinstance(value, str):
        raise TypeError("Pass in strings with keyword hexstr or text")
    else:
        return int(value)