Reputation: 145
I have created a Twitter bot based on Google Apps Script and wolfram|alpha. The bot answers questions the way wolfram|alpha does.
I need to translate a string from English to Devanagari.
I get result as \:0936\:093e\:092e
which should be converted to "शाम"
Here is a link for more info - https://codepoints.net/U+0936?lang=en
I want to know how I can achieve this using Google Apps Script (JavaScript)?
Upvotes: 3
Views: 472
Reputation: 45750
The values in \:0936\:093e\:092e
are UTF-16 character codes, but are not expressed in a way that will render the characters you need. If they were, you could use the answer from Expressing UTF-16 unicode characters in JavaScript directly.
This script extracts the hexadecimal numbers from the given string, then uses the getUnicodeCharacter()
function from the linked question to convert each number, or codepoint, into its Unicode character.
function utf16demo() {
var str = "\:0936\:093e\:092e";
var charCodes = str.replace(/\:/,'').split('\:').map(function(st){return parseInt(st,16);});
var newStr = '';
for (var ch=0; ch < charCodes.length; ch++) {
newStr += getUnicodeCharacter(charCodes[ch])
}
Logger.log(newStr);
}
[15-11-03 23:04:16:096 EST] शाम
Upvotes: 2