Linked Questions
30 questions linked to/from Why does "bytes(n)" create a length n byte string instead of converting n to a binary representation?
2 votes
1 answer
5k views
How to encode string to bytes in python? [duplicate]
I want to take an integer, that will be between 0 and 255, convert that to a hex string eg. '\xff' and then cast that to bytes to end up with b'\xff'. I had assumed that the following would work. ...
1 vote
1 answer
5k views
Convert arbitrary int to bytes [duplicate]
How can I use struct to convert an arbitrary int (unsigned, signed, long long,... just int in Python) to a byte sequence (bytes)? If I read this correctly, I would need to decide on a format string ...
1 vote
2 answers
2k views
In Python, how can I convert an integer between 0 and 255 to a single unsigned byte? [duplicate]
My laptop runs a Python program that reads a joystick to send a speed command to an Arduino that in turn controls the motor on a toy car. To do that the program converts an integer with values between ...
-1 votes
1 answer
156 views
Python: Convert hex notation to binary string [duplicate]
I have a binary format specification. The specification lists tags/objects in hex notation like 0x1000 which in the actual file when read into bytes in python is the binary string b'\x00\x10'. There ...
0 votes
1 answer
69 views
Convert a signed long to ASCII binarized hexidecimal format python [duplicate]
Given a 8-bytes long signed value, like 3576757170468630901, I would like to convert it to ASCII binarized hexadecimal: For example: >> hex(3576757170468630901).encode('ascii') b'...
0 votes
0 answers
93 views
Converting int to bytes [duplicate]
I would like to convert long into bytes in Python 3. Following code I'm using in C#: long time = 7620584977 var a = BitConverter.GetBytes(time).Reverse().ToArray() // a is [0,0,0,1,198,56,230,224] ...
0 votes
0 answers
38 views
Convert list of numbers to list of length 1 byte objects [duplicate]
I'm attempting to convert a list of numbers into a list of byte objects of length 1 for use with struct.pack_into. However, much to my surprise, I appear to be getting n 0s in each byte object: >&...
87 votes
9 answers
183k views
Convert a Python int into a big-endian string of bytes
I have a non-negative int and I would like to efficiently convert it to a big-endian string containing the same data. For example, the int 1245427 (which is 0x1300F3) should result in a string of ...
88 votes
6 answers
117k views
Python 3 bytes formatting
In Python 3, one can format a string like: "{0}, {1}, {2}".format(1, 2, 3) But how to format bytes? b"{0}, {1}, {2}".format(1, 2, 3) raises AttributeError: 'bytes' object has no attribute 'format'. ...
35 votes
5 answers
50k views
How to use the 'hex' encoding in Python 3.2 or higher?
In Python 2, to get a string representation of the hexadecimal digits in a string, you could do >>> '\x12\x34\x56\x78'.encode('hex') '12345678' In Python 3, that doesn't work anymore (tested ...
10 votes
2 answers
5k views
Use Python's `timeit` from a program but functioning the same way as the command line?
For instance, documentation says: Note however that timeit will automatically determine the number of repetitions only when the command-line interface is used. Is there a way to call it from within ...
8 votes
2 answers
9k views
Python byte literal
I supect this a bit of a python newbie question, but how do you write a single byte (zero in this case) to a file? I have this code: f = open("data.dat", "wb") f.write(0) f.close() But on the write ...
1 vote
2 answers
4k views
How to convert and save a list of ints to a bitmap image?
I'm trying to convert a list of numbers that I believe represent bytes that together constitute a bitmap image, into said image file (saved to disk) and/or simply converted into a form usable by ...
5 votes
1 answer
5k views
int.to_bytes length calculation
Probably a stupid question but I stumbled across the int.to_bytes() function and I can't explain why I have to add "+7" if I calculate the bytes length and there is no hint in the ...
0 votes
4 answers
2k views
Python: Convert number in a list of bytes
I have to convert a number to a list depending on its decimal value. For example, the number 300(0x012C) would be [1, 44] because 1 and 44 are 0x01 and 0x2c respectively. How can I do that?