In Python 3.2+, you can use int.to_bytes:
If you don’t want to specify the size
>>> n = 1245427
>>> n.to_bytes((n.bit_length() + 7) // 8, 'big') or b'\0'
b'\x13\x00\xf3'
If you don’t mind specifying the size
>>> (1245427).to_bytes(3, byteorder="big")
b'\x13\x00\xf3'
More Related Contents:
- How do I parse a string to a float or int?
- How do I get the last element of a list?
- How to find all occurrences of a substring?
- Python Variable Declaration
- Weird Try-Except-Else-Finally behavior with Return statements
- Python find elements in one list that are not in the other [duplicate]
- Pandas: Get Dummies
- Repeating each element of a numpy array 5 times
- Nested SSH using Python Paramiko
- Python – difference between two strings
- Python parsing bracketed blocks
- How do I write good/correct package __init__.py files
- Format string unused named arguments [duplicate]
- How to use chrome webdriver in selenium to download files in python?
- problems with pygame controller support
- Python pandas / matplotlib annotating labels above bar chart columns [duplicate]
- How to upsert pandas DataFrame to PostgreSQL table?
- error installing nltk supporting packages : nltk.download()
- How can I check if two strings are anagrams of each other?
- Inserting the same value multiple times when formatting a string
- UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xd1 in position 2: ordinal not in range(128)
- multiprocessing pool example does not work and freeze the kernel
- ‘virtualenv’ is not recognized as an internal or external command, operable program or batch file
- Checking if a website is up via Python
- Write a Pandas DataFrame to Google Cloud Storage or BigQuery
- Weighted random sample without replacement in python
- How to create raw string from string variable in python?
- ValueError: Input 0 is incompatible with layer lstm_13: expected ndim=3, found ndim=4
- How to create 3D scatter animations
- Python: Return 2 ints for index in 2D lists given item