Last active
February 10, 2017 07:56
-
-
Save quietcricket/179d20a39dbbdf92cb3b9cdc7af2d11c to your computer and use it in GitHub Desktop.
Convert unicode octal sequence to Javascript usable string
This file contains hidden or 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
/** | |
* An Octal Escape Sequence looks like this \345\225\206 | |
* The unicode hex value is 0xE5 0x95 0x86 | |
* The octal value 345 is hexal value E5 | |
* The octal value 225 is hexal value 95 | |
* The octal value 206 is hexal value 86 | |
* This function converts octal values to hexal values first | |
* Then join the hexal values into an url escape code %E5%95%86, | |
* which HTML/Javascript is able to output properly | |
* WARNING: not the safest method. If the string has \xxx but not | |
* part of the octal escape sequence, result will not be correct. | |
* The chance of this happening is very low, but possible. | |
**/ | |
function decode_octal(input){ | |
var oct_to_hex = function (oct) { | |
// Converts \345 into %E5 | |
// Supposed to be an inline function, or lambda but I don't know how to write in JS (not interested) | |
var hex = parseInt(oct.substr(1), 8).toString(16); | |
if (hex.length == 1) { | |
hex = '0' + hex; | |
} | |
return '%' + hex.toUpperCase(); | |
}; | |
// look for \xxx | |
var temp = input.match(/\\\d{3}/g); | |
if (temp === null||temp.length<3) { | |
return input | |
} | |
for (var i = 0; i < temp.length / 3; i++) { | |
if (i * 3 + 2 > temp.length) { | |
break; | |
} | |
var c1 = temp[i * 3]; | |
var c2 = temp[i * 3 + 1]; | |
var c3 = temp[i * 3 + 2]; | |
var uc = oct_to_hex(c1) + oct_to_hex(c2) + oct_to_hex(c3); | |
input = input.replace(c1 + c2 + c3, uc); | |
} | |
return decodeURI(input); | |
} | |
// An example | |
decode_octal('\345\225\206\351\207\217'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment