Different representation of class 'bytes' in python 3 [duplicate] - python

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'

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

Bytes output from Python [duplicate]

This question already has answers here:
Writing binary files in Python 3, why I'm not getting the hexadecimal representation of 9,10 and 13?
(1 answer)
Convert 10 to bytes, strange result
(1 answer)
Closed 2 years ago.
I am using bytes in python to convert following integers into bytes:
>>> b = bytes([192, 168, 10, 254])
I get the following output:
b'\xc0\xa8\n\xfe'
My question, why does the int 10 is represented as \n and not hex value of '0xa'.
Looking at the output, 192, 168 and 254 all gets represented as their equilvalent hex values.
Why not 10 is represented by its hex value.
Thanks

Convert binary back to ascii in python [duplicate]

This question already has answers here:
Convert binary to ASCII and vice versa
(8 answers)
Closed 4 years ago.
So i have a function that converts an alphabet character to it's binary.
def toBinary(char):
return "".join([format(ord(char), '#010b')[2:]])
For example, toBinary('a') gives me
01100001
How do i convert 01100001 back to the ascii 'a'?
One way could be
c = chr(int(s, 2))
where s is the binary string.
Try This:
chr(int('01100001',2))

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 String into Float? [duplicate]

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'>

Categories