1

I read some value from Windows Registry (SAM) with Python3. As far as I can tell it looks like hex encoded bytes:

    >>> b = b'A\x00d\x00m\x00i\x00n\x00i\x00s\x00t\x00r\x00a\x00t\x00o\x00r\x00'
    >>> print(b)
    A d m i n i s t r a t o r

Now how would I convert that to a String (should be "Administrator")? Using "print" just gives me "A d m i n i s t r a t o r". How to do the conversion correctly without using dirty tricks?

2 Answers 2

1
b = b'A\x00d\x00m\x00i\x00n\x00i\x00s\x00t\x00r\x00a\x00t\x00o\x00r\x00'
b = b.replace(b'\x00', b'')
print(b)
# b'Administrator'
Sign up to request clarification or add additional context in comments.

2 Comments

Sure it works, but is this the proper way to do it?
Every string in ASCII ends at a NULL ('\x00'). You need to get rid of them.
0

I propably should have used utf-16 decoding:

    >>> b = b'A\x00d\x00m\x00i\x00n\x00i\x00s\x00t\x00r\x00a\x00t\x00o\x00r\x00'
    >>> print(b.decode('utf-16'))
    Administrator

SORRY!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.