0

After receiving the bytes from Server, it needs to convert into string. When I try below code, not works per expected.

a
Out[140]: b'NC\x00\x00\x00'

a.decode()
Out[141]: 'NC\x00\x00\x00'

a.decode('ascii')
Out[142]: 'NC\x00\x00\x00'

a.decode('ascii').strip()
Out[143]: 'NC\x00\x00\x00'

a.decode('utf-8').strip()
Out[147]: 'NC\x00\x00\x00'

# I need the Output as 'NC'
0

1 Answer 1

2

This is not an encoding issue, as the trailing bytes are all NUL bytes. Looks like your server is padding with Null bytes. To remove them just use

a.strip(b'\x00')
Sign up to request clarification or add additional context in comments.

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.