This question already has answers here:
How to convert a string of bytes into an int?
(12 answers)
Closed 4 years ago.
I know that to convert from int to bytes you have to do this, for example:
>>>a = bytes(4)
>>>print(a)
b'\x00\x00\x00\x00'
But what do I do if I want to revert it and convert bytes to int or float?
I tried using:
int.from_bytes( a, byteorder='little')
and
int.from_bytes( a, byteorder='big', signed=True)
but it did not work.
import struct
val = struct.unpack( '<I', b'\x00\x00\x00\x00')[0]
or something along the lines... control the big/little with < or > sign.
Here are the docs: 7.1. struct — Interpret bytes as packed binary data
Related
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
This question already has answers here:
OverflowError: Python int too large to convert to C long
(3 answers)
Closed 3 years ago.
When I use pandas.DataFrame.replace(dict) to convert user_id string to integer, I receive:
"OverflowError: Python int too large to convert to C long".
sample code:
import pandas as pd
x = {'user_id':['100000715097692381911',
'100003840837471130074'],
'item_id': [1, 2]
}
dfx = pd.DataFrame(x)
dfx['user_id'].replace(
{
'100000715097692381911': 0,
'100003840837471130074': 1
}, inplace=True)
I don't understand why this is duplicated. I think this is a problem of pandas taking str type as integers. I didn't load those big id numbers as integer but as string. Well, if I prepend an character to 'user_id' string, like 's100000715097692381911', it will not report OverflowError.
In C, a long is 4 bytes and can only store values between -2,147,483,648 and 2,147,483,647.
To answer your other question, a string in C is stored as a char array, and so it's memory space is 1 byte for each char, plus the size of the terminating pointer. This means a python string in C won't cause an overflow, but a large integer will.
Source: https://www.tutorialspoint.com/cprogramming/c_data_types.htm
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'
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))
This question already has answers here:
Convert hex string to integer in Python
(10 answers)
Closed 8 years ago.
I have some Perl code where the hex() function converts hex data to decimal. How can I do it on Python?
If by "hex data" you mean a string of the form
s = "6a48f82d8e828ce82b82"
you can use
i = int(s, 16)
to convert it to an integer and
str(i)
to convert it to a decimal string.
>>> int("0xff", 16)
255
or
>>> int("FFFF", 16)
65535
Read the docs.
You could use a literal eval:
>>> ast.literal_eval('0xdeadbeef')
3735928559
Or just specify the base as argument to int:
>>> int('deadbeef', 16)
3735928559
A trick that is not well known, if you specify the base 0 to int, then Python will attempt to determine the base from the string prefix:
>>> int("0xff", 0)
255
>>> int("0o644", 0)
420
>>> int("0b100", 0)
4
>>> int("100", 0)
100