Convert a signed long to ASCII binarized hexidecimal format python [duplicate] - python

This question already has answers here:
Why does "bytes(n)" create a length n byte string instead of converting n to a binary representation?
(16 answers)
Closed 2 years ago.
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'0x31a331b2319d3175'
What I am looking for is the following format:
b'\x31\xa3\x31\xb2\x31\x9d\x31\x75'
I am not sure how can I generate it? Should I break each block and convert it myself?

In python 3 there is now to_bytes() which may help here:
v = 3576757170468630901
print(hex(v))
print(v.to_bytes(8, 'big'))
Output:
0x31a331b2319d3175
b'1\xa31\xb21\x9d1u'

Related

Insert '\x' in python3 [duplicate]

This question already has answers here:
How to convert hexadecimal string to bytes in Python?
(7 answers)
Closed 11 months ago.
I am trying to use crcmod correctly but I have problem:
For example I would like to transform the string "1234567809" into the bytes b'\x12\x34\x56\x78\x09' in order to obtain the correct crc16 modbus code.
import crcmod
crc16 = crcmod.mkCrcFun(0x18005, rev=True, initCrc=0xffff)
for i in range(len(a)//2):
a = a[:(i*4)] + r"\x" + a[i*4:]
The problem is that r"\x" inserts '\\x', not '\x',
and of course '\x' returns an error.
a.encode("utf-8")
of course returns b'\\x12\\x34\\x56\\x78\\x09'.
That is the result with \\x:
hex(crc16(a.encode("utf-8")))
'0x68b7'
That is the result I expected:
hex(crc16(b'\x12\x34\x56\x78\x09'))
'0x2590'
You can directly convert a string: '1234567809' to the bytes you want:
source = '1234567809'
a = bytes.fromhex(source)
Now the crc should work:
hex(crc16(a))

Getting value component in python? [duplicate]

This question already has answers here:
Python - Split integer into individual digits (undetermined amount of digits)
(4 answers)
Closed 1 year ago.
I cannot describe clearly because English is not my native language
If I input like this
a = 4252
Then I want to take each number component, it should be like this
a1=4; a2=2; a3=5; a4=2
How to do that in python?
If you know it has a similar question in stackoverflow, give me a link
Convert the integer to a string for easy iterability then get the int value of every character like this: a1,a2,a3,a4 = [int(elem) for elem in str(a)]

How to reverse the float.hex() method in Python [duplicate]

This question already has answers here:
Converting hex string representation to float in python
(2 answers)
Closed 2 years ago.
Is there a way to reverse the hex() method of a float in Python? For example,
n = 1280.03125
n_hex = n.hex()
print(n_hex) # result--> 0x1.4002000000000p+10
How can I convert 0x1.4002000000000p+10 back to 1280.03125? I know you can use int(num, base) to convert a number to integer but it doesn't support decimal.
Try float.fromhex(str):
>>> float.fromhex("0x1.4002000000000p+10")
1280.03125

Hex Color generator RGB in Python [duplicate]

This question already has answers here:
Converting an RGB color tuple to a hexidecimal string
(16 answers)
Closed 2 years ago.
Can anyone help me with this exercise in python?
ASK:
Create a function that accepts three integers that represent the RGB values and outputs the hex-code representation.
SAMPLE
100
200
233
SAMPLE OUTPUT
#64c8e9
Use the built-in format operator
print('#%02x%02x%02x' % (100,200,233))
will output #64c8e9

How to convert int to 4 byte hex [duplicate]

This question already has an answer here:
Converting a value into 4 byte hex in python
(1 answer)
Closed 6 years ago.
I need to convert an unsigned integer like 4003 to its hex representation. The output of the following code
print(struct.pack("<I", 4003).encode('hex'))
is
a30f0000
How can I get the following output?
00000fa3
It's not necessary to use struct.pack. Any other approach would be appreciated.
>>> '{:08x}'.format(4003)
'00000fa3'
You can get your desired result using
print(struct.pack(">I", 4003).encode('hex'))

Categories

Resources