I have the following problem in python
I have the value 0x402de4a in hex and would like to convert it to bytes so I use.to_bytes(3, 'little') which gives me b'J\2d@'
if I print it. I am aware that this is just a representation of the bytes but I need to turn a string later for the output which would give me J\2d@
if I use str() nut I need it to be \x4a\x2d\x40
how can I convert the byte object to string so I can get the raw binary data as a string
my code is as follows
addr = 0x402d4a
addr = int(addr,16)
addr = str(addr.to_bytes(3,'little'))
print(addr)
and my expected output is \x4a\x2d\x40
Thanks in advance
There is no direct way to get \x4a\x2d
and so forth from a string. Or bytes, for this matter.
What you should do:
"\\x"
prefixjoin()
them 2 & 3 can nicely be folded into one generator comprehension, eg:
rslt = "".join(
f"\\x{b:02x}" for b in value_as_bytes
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.