Python fastest byte to string conversion [duplicate] - python

This question already has answers here:
Convert bytes to a string
(22 answers)
Closed 8 years ago.
I need to convert a byte array to a string to send to an SPI device.
Is there a more efficient way of doing this ?
def writebytes(bytes):
str = ""
for i in bytes: str += chr(i)
self.spi.transfer(str)

Use "".join with a generator expression.
def writebytes(bytes):
self.spi.transfer("".join(chr(i) for i in bytes))

Related

how to convert the bytes to int64 in the python [duplicate]

This question already has answers here:
Convert bytes to int?
(7 answers)
Closed 2 years ago.
My question is:
how can we convert the bytes to int64 in python
in C# we could use BitConverter.ToInt64()for transfer the bytes to int64.
but I didn't find similar function in the python.
how can I do it in the python. I just find the int.from_bytes().
input: System.Byte[], \x12\x77\x2b\xca\x9b\x62\xa2\x72\x9e\xc8\xb7\xa7\x82\xd8\x4c\xba\xcb\x41\x78\x4c\x5a\x72\xdd\xf6
output: 4666902099556679087
In python, there is only int and no int32 and int64. You can easily convert bytes string to int (supposing you are using only builtin types and not e.g. numpy):
bytesstr = bytes("123") # the bytes string
numstr = bytesstr.decode() # convert bytes string to normal string
num = int(numstr) # convert normal string to number

How to convert bytearray to string in python [duplicate]

This question already has answers here:
How to convert bytearray with non-ASCII bytes to string in python?
(4 answers)
Closed 4 years ago.
I need to convert the next bytearray to string:
Num = bytearray()
I have tried
Num = bytearray(str)
but that's not the solution
As t.m.adam said in the comments, use bytearray.decode
b = bytearray("test", encoding="utf-8")
b.decode()
#> 'test'

String to Int in Python [duplicate]

This question already has answers here:
Why does map return a map object instead of a list in Python 3?
(4 answers)
Getting a map() to return a list in Python 3.x
(11 answers)
Closed 5 years ago.
How to Convert a string like '123' to int 1,2 and 3 so that I can perform 1+2+3. I am new to python. Can you please help me? I am not able to split the list. I don't think splitting the string will be of any use as there are no delimiters. Can you help me to understand how can this string elements be separated and treated as intergers?
x = "123"
s = 0
for a in x:
s = int(a) + s

Python Binary string to ASCII text [duplicate]

This question already has answers here:
Convert binary to ASCII and vice versa
(8 answers)
Closed 6 years ago.
x = "01100001"
How do i convert this string into ASCII
Expected Result:
print(x.somefunction())
Output: a
Use following one liner
x = chr(int('010101010',2))
will work.

How to convert str to hex via python [duplicate]

This question already has answers here:
Print a string as hexadecimal bytes
(13 answers)
Closed 7 years ago.
I want to convert string to a hex (decimal),
for example abc to 616263 and so on.
How do I do that?
I have tried:
x= input("Enter string")
x = int(x)
hex(x)
But it is not working.
maybe you are looking for something like:
x.encode("hex")

Categories