Reputation: 253
When I convert the a string in 04x hex it will be like this,
"".join("{:04x}".format(ord(c)) for c in "USERDOMAIN")
==> 00550053004500520044004f004d00410049004e
However I need it in little endian format? In this example
==> 550053004500520044004f004d00410049004e00
How should I calculate this value (I mean little endian format of USERDOMAIN 04x hex) in python?
Upvotes: 1
Views: 1762
Reputation: 1124170
If you are trying to encode to UTF16 bytes, just do so:
u'USERDOMAIN'.encode('utf-16-le')
which encodes to UTF-16, little endian, without a Byte Order Mark:
>>> u'USERDOMAIN'.encode('utf-16-le')
'U\x00S\x00E\x00R\x00D\x00O\x00M\x00A\x00I\x00N\x00'
>>> u'USERDOMAIN'.encode('utf-16-le').encode('hex')
'550053004500520044004f004d00410049004e00'
Note that I created a Unicode literal by prefixing the string definition with u
. If you have a bytestring from another source, you'll have to decode to Unicode first unless that data is ASCII only.
Upvotes: 1