i can convert an int to bytes by first converting to hexadecimal characters (and maybe prefixing a '0' to make an even number of digits). is there any more direct way to convert an int to the binary equivalent sequence of bytes without doing the hexadecimal conversion and/or adjusting the number of hexadecimal digits to a multiple of two.
for those of you that like to have code you can squish in your fingers...
bytes_to_int.py:
int_to_bytes.py:
the above code is not tested, yet. i may test it, some day.
for those of you that like to have code you can squish in your fingers...
bytes_to_int.py:
1 2 3 4 5 6 7 8 |
t = bytes,bytearray def bytes_to_int(bytes = None ): """Convert bytes or bytearray to int.""" if bytes is None : return None if isinstance (bytes,t): return int .from_bytes(bytes) raise TypeError( 'arg 1 type must be bytes or bytearray' ) |
1 2 3 4 5 6 7 8 9 10 11 |
t = int def int_to_bytes( int = None ): """Convert int to bytes.""" if int is None : return None if isinstance ( int ,t): h = hex ( int )[ 2 :] if len (h) % 2 : h = '0' + h return bytes.fromhex(h) raise TypeError( 'arg 1 type must be int' ) |
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.