0

can anyone please help me with following issue:

I have a str1 defined locally as str1 = 'CV\xca\x86\x11\x85\x01\xc8' and str2 received from another layer of code:

print str2 gives 'CV\xca\x86\x11\x85\x01\xc8' so they look identical, however:

print binascii.hexlify(str1) gives 4356ca86118501c8 or INT: 4852288325706645960

but for str2

print binascii.hexlify(str2) gives 43565c7863615c7838365c7831315c7838355c7830315c786338 or int 108206947078930905153039906183652663420044626270557263434900280

The problem seems to be probably different encoding where

print chardet.detect(str1) gives me my local encoding: {'confidence': 0.73, 'language': '', 'encoding': 'Windows-1252'}

print chardet.detect(str2) gives {'confidence': 1, 'language': '', 'encoding': 'ascii'}

How can I modify the str2 to get the same hex or int values from it like for str1?

0

1 Answer 1

1

You have a string with literal backslashes, 'x' characters and hex digits:

>>> from binascii import unhexlify
>>> unhexlify('43565c7863615c7838365c7831315c7838355c7830315c786338')
'CV\\xca\\x86\\x11\\x85\\x01\\xc8'

The representation of the string doubles the backslashes, so you can reproduce the value. The representation of the other string has no such doubling, because the \xhh sequences each form a single character:

>>> unhexlify('4356ca86118501c8')
'CV\xca\x86\x11\x85\x01\xc8'

Compare the individual characters:

>>> 'CV\xca\x86\x11\x85\x01\xc8'[2]
'\xca'
>>> 'CV\\xca\\x86\\x11\\x85\\x01\\xc8'[2]
'\\'

You can decode escape sequences with the string_escape codec:

>>> from binascii import hexlify
>>> 'CV\\xca\\x86\\x11\\x85\\x01\\xc8'.decode('string_escape')
'CV\xca\x86\x11\x85\x01\xc8'
>>> hexlify('CV\\xca\\x86\\x11\\x85\\x01\\xc8'.decode('string_escape'))
'4356ca86118501c8'
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.