Last active
August 29, 2015 14:12
-
-
Save akecn/a9f02f05f3add13de39d to your computer and use it in GitHub Desktop.
native2ascii
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ascii2native(str) { | |
return unescape(str.split('\\').join('%')); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function native2ascii(str) { | |
var character = str.split(''); | |
var ascii = ''; | |
for (var i = 0; i < character.length; i++) { | |
var code = Number(character[i].charCodeAt(0)); | |
if (code > 127) { | |
var charAscii = code.toString(16); | |
charAscii = new String('0000').substring(charAscii.length, 4) + charAscii; | |
ascii += '\\u' + charAscii; | |
}else { | |
ascii += character[i]; | |
} | |
} | |
return ascii; | |
} | |
function ascii2native(str) { | |
return unescape(str.split('\\').join('%')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment