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
Related
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 hexadecimal string to bytes in Python?
(7 answers)
Closed 4 years ago.
I have two variables of the class bytes in python3.
print(string1) --> b'2900BCE03604093C000080'
print(bytes.fromhex(string1.decode('utf8'))) --> b')\x00\xbc\xe06\x04\t<\x00\x00\x80'
print(type(string1)) --> <class 'bytes'>
print(type(bytes.fromhex(string1.decode('utf8')))) --> <class 'bytes'>
The strange values in the second output are there because of ascii interpretation of some hex-values.
My Question is how to convert the string1 more easily to the output of the second line. Is there a better way?
You can use binascii.a2b_hex() function to get the hexadecimal representation of the binary data:
In [5]: binascii.a2b_hex(s)
Out[5]: b')\x00\xbc\xe06\x04\t<\x00\x00\x80'
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
This question already has answers here:
How do I parse a string to a float or int?
(32 answers)
Closed 6 years ago.
I'm required to covert the variable:
pi_string = "3.1415926"
into a float. Here's what I'm dealing with:
Your line should be pi_float = float(pi_string)
float(pi_string) is a float value, you can not assign to it, because it is not a variable.
The method float() will do this for you if pi_string = "3.1415926".
>>>pi_float = float(pi_string)
>>>type(pi_float)
<class 'float'>
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))