JavaScript
x
6
1
a = 'xe6xb8xacxe8xa9xa6'
2
print(bytes(a, 'latin-1').decode('utf-8'))
3
4
a = input("input:")
5
print(bytes(a, 'latin-1').decode('utf-8'))
6
The first one can print out the result correctly
While the second one will just print out the string I entered
output:
JavaScript
1
7
1
測試
2
input:xe6xb8xacxe8xa9xa6
3
xe6xb8xacxe8xa9xa6
4
5
Process finished with exit code 0
6
7
Advertisement
Answer
The transformation is a bit tricky:
JavaScript
1
5
1
# Use r'', simulate input
2
a = r'xe6xb8xacxe8xa9xa6'
3
4
print(a.encode('ascii').decode('unicode-escape').encode('latin-1').decode('utf-8'))
5
Follow the transformation:
JavaScript
1
20
20
1
# Step 0 (initial)
2
print(a)
3
xe6xb8xacxe8xa9xa6
4
5
# Step 1
6
print(a.encode('ascii'))
7
b'\xe6\xb8\xac\xe8\xa9\xa6'
8
9
# Step 2
10
print(a.encode('ascii').decode('unicode-escape'))
11
測試
12
13
# Step 3
14
print(a.encode('ascii').decode('unicode-escape').encode('latin-1'))
15
b'xe6xb8xacxe8xa9xa6'
16
17
# Step 4 (final)
18
print(a.encode('ascii').decode('unicode-escape').encode('latin-1').decode('utf-8'))
19
測試
20