유호영
유호영

Reputation: 11

How do i convert python encode decode

Why are the results different?

# fabric
subject = run(openssl x509 -subject -noout -in signCert.pem)
subject = subject.split(b"CN = ")[1] # '\EC\A3\BC\EC\8B\9D\ED\9A\8C\EC\82\AC' <= type string

# FAIL
subject = '\EC\A3\BC\EC\8B\9D\ED\9A\8C\EC\82\AC'.replace('\\','\\x')
print(subject.encode('latin1').decode()) #\xEC\xA3\xBC\xEC\x8B\x9D\xED\x9A\x8C\xEC\x82\xAC

I want to decode like below

# Success

a = '\xEC\xA3\xBC\xEC\x8B\x9D\xED\x9A\x8C\xEC\x82\xAC'
print(a.encode('latin1').decode()) # 주식회사

How to convert this string? '\EC\A3\BC\EC\8B\9D\ED\9A\8C\EC\82\AC' -> b'\xEC\xA3\xBC\xEC\x8B\x9D\xED\x9A\x8C\xEC\x82\xAC'

Upvotes: 0

Views: 206

Answers (1)

Grismar
Grismar

Reputation: 31339

One way to do what you're asking:

s = '\EC\A3\BC\EC\8B\9D\ED\9A\8C\EC\82\AC'

result = eval('b"{}"'.format(s.replace("\\", "\\x")))

print(result)
print(result.decode())

Result:

b'\xec\xa3\xbc\xec\x8b\x9d\xed\x9a\x8c\xec\x82\xac'
주식회사

However, it is likely that there's a better way to achieve this avoiding all the conversion, if you deal differently with the output from the run() command.

The reason the code above works is because it evaluates the new string as it would if the expression had been in code - which is what you're trying to do.

Another (safer) approach would be to split the string over backslashes, convert the hexadecimal into numerical values and construct a bytes object from that:

result2 = bytes(int(x, 16) for x in s[1:].split('\\'))

print(result2)
print(result2.decode())

Same result. This assumes you can be sure s starts with a backslash, hence the [1:].

Upvotes: 4

Related Questions