I'm trying to obfuscate some javascript by altering their character codes, but I've found that I can't correctly print characters outside of a certain range, in Python 2.7.
For example, here's what I'm trying to do:
f = open('text.txt','w')
f.write(unichr(510).encode('utf-8'))
f.close()
I can't write unichr(510) because it says the ascii codec is out of range. So I encode it with utf-8. This turns a single character u'\u01fe'
into two '\xc7\xbe'
.
Now, in javascript, it's easy to get the symbol for the character code 510:
String.fromCharCode(510)
Gives the single character: Ǿ
What I'm getting with Python is two characters: Ǿ
If I pass those characters to javascript, I can't retrieve the original single character.
I know that it is possible to print the Ǿ character in python, but I haven't been able to figure it out. I've gotten as far as using unichr() instead of chr(), and encoding it to 'utf-8', but I'm still coming up short. I've also read that Python 3 has this functionality built-in to the chr() function. But that won't help me.
Does anyone know how I can accomplish this task?
Thank you.
'\xc7\xbe'
to JavaScript? Those two consecutive bytes (not to be confused with the characters Ǿ) are the UTF-8 encoding of Ǿ, which JavaScript should recognize as such (or at least treat no differently than a Ǿ appearing in a UTF-8 encoded JS file). – jwodder'\xc7\xbe'
to a javascript file. Also, it is treating it as two separate characters. @jwodder – bozdoz