Home Python Unicode Casting on Variable Bug
Reply: 0

Python Unicode Casting on Variable Bug

user1130
1#
user1130 Published in June 23, 2018, 7:53 am

I've found out this weird python2 behavior related to unicode and variable:

>>> u"\u2730".encode('utf-8').encode('hex')
'e29cb0'

This is the expected result I need, but I want to dynamically control the first part ("u\u2730")

>>> type(u"\u2027")
<type 'unicode'>

Good, so the first part is casted as unicode. Now declaring a string variable and casting it to unicode:

>>> a='20'
>>> b='27'
>>> myvar='\u'+a+b.decode('utf-8')
>>> type(myvar)
<type 'unicode'>
>>> print myvar
\u2027

It seems that now I can use the variable in my original code, right?

>>> myvar.encode('utf-8').encode('hex')
'5c7532303237'

The results, as you can see, is not the original one. It seems that python is treating 'myvar' as string instead of unicode. Do I miss something?

Anyway, my final goal is to loop Unicode from \u0000 to \uFFFF, cast them as string and cast the string as HEX. Is there an easy way?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.312084 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO