Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove \x in string in python

Tags:

python


I need get little endian random string via python.
Such as '0010' as 256 (without '\x')
Here is my code.

 import random
 import struct
 str1 = struct.pack('<Q', random.randint(1, 1000))
 #Ex: str1 = '\xc9\x02\x00\x00\x00\x00\x00\x00'

But I don't have any idea to convert this string to "C902000000000000" Please give any advise, thanks

like image 496
Evan Lin Avatar asked Dec 18 '25 14:12

Evan Lin


1 Answers

Another alternative, which is quite easy to type, is the following approach:

>>> import random
>>> import struct
>>> str1 = struct.pack('<Q', random.randint(1, 1000))
>>> str1.encode('hex')
like image 199
glglgl Avatar answered Dec 21 '25 04:12

glglgl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!