Created
May 30, 2016 10:05
-
-
Save lzl124631x/79f502cd4fa70d2637f5169baf8b0a86 to your computer and use it in GitHub Desktop.
Encode to unicode
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
String.prototype.toUnicode = function(){ | |
var result = ""; | |
for(var i = 0; i < this.length; i++){ | |
result += "\\u" + ("000" + this[i].charCodeAt(0).toString(16)).substr(-4); | |
} | |
return result; | |
}; | |
"みどりいろ".toUnicode(); //"\u307f\u3069\u308a\u3044\u308d" | |
"Mi Do Ri I Ro".toUnicode(); //"\u004d\u0069\u0020\u0044\u006f\u0020\u0052\u0069\u0020\u0049\u0020\u0052\u006f" | |
"Green".toUniCode(); //"\u0047\u0072\u0065\u0065\u006e" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment