HexInt

Fixed-width integer

Display ints of any any size as hex


source

hexint

 hexint (value, bits)

Returns the value as a hex string (with leading 0s, without 0x prefix)

hexint(123, 8), hexint(-123, 8), hexint(123, 32), hexint(-123, 128), hexint(-1, 256), hexint(0x1234567890abcdef, 64)
('7B',
 '85',
 '0000007B',
 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF85',
 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF',
 '1234567890ABCDEF')

And as ascii


source

asciiint

 asciiint (value:int, bits:int=8)
asciiint(0x48656c6c6f20576f726c64, 128), asciiint(0x1234567890abcdef, 64), asciiint(70, 8), asciiint(-123, 64)
('.....Hello World', '.4Vx....', 'F', '........')

A class allows us to pack the value and the bits information together,


source

HexInt

 HexInt (value:int|None, bits:int=32, signed:bool=False)

*int([x]) -> integer int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x.__int__(). For floating point numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in the given base. The literal can be preceded by ‘+’ or ‘-’ and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal. >>> int(‘0b100’, base=0) 4*

HexInt(123, bits=8)
0x7B
HexInt(-123, bits=8, signed=True)
0x85 (-123)
HexInt(0x1234567890abcdef, bits=64).ascii()
'.4Vx....'
HexInt(-123456789013443242, bits=64, signed=True)
0xFE4964B459BE4D56 (-123456789013443242)
HexInt.from_ctype(ctypes.c_int64(-123456789013443242))
0xFE4964B459BE4D56 (-123456789013443242)

This is useful for ctypes enum members


source

make_named_int

 make_named_int (name, val)

source

NamedInt

 NamedInt (val)

*int([x]) -> integer int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x.__int__(). For floating point numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in the given base. The literal can be preceded by ‘+’ or ‘-’ and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal. >>> int(‘0b100’, base=0) 4*

make_named_int("TEST", 123)
TEST(123)
make_named_int("TEST", HexInt(123, bits=8, signed=True))
TEST(0x7B)