Linked Questions

2 votes
1 answer
5k views

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. ...
Christopher Ryan's user avatar
1 vote
1 answer
5k views

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 ...
orange's user avatar
  • 8,252
1 vote
2 answers
2k views

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 ...
Daanii's user avatar
  • 259
-1 votes
1 answer
156 views

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 ...
beginner_'s user avatar
  • 7,652
0 votes
1 answer
69 views

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'...
Coderji's user avatar
  • 7,785
0 votes
0 answers
93 views

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] ...
muradheydarov's user avatar
0 votes
0 answers
38 views

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: >&...
gandalf3's user avatar
  • 1,676
87 votes
9 answers
183k views

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 ...
fish's user avatar
  • 2,030
88 votes
6 answers
117k views

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'. ...
Ecir Hana's user avatar
  • 11.6k
35 votes
5 answers
50k views

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 ...
Tim Pietzcker's user avatar
10 votes
2 answers
5k views

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 ...
endolith's user avatar
  • 27k
8 votes
2 answers
9k views

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 ...
Martin Brown's user avatar
  • 25.5k
1 vote
2 answers
4k views

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 ...
weirdalsuperfan's user avatar
5 votes
1 answer
5k views

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 ...
user avatar
0 votes
4 answers
2k views

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?
itsp's user avatar
  • 11

15 30 50 per page