Javascript unicode decoder and encoder u00XX

Here is a javascript method to encode and decode the unicode characters. This is something i created to modify the URLs for facet links returned from an e-commerce search engine.


Javascript unicode decoder

function simple_unicode_decode(str) {
str=str.replace("u0001","âº");
str=str.replace("u0002","â»");
str=str.replace("u0003","â¥");
str=str.replace("u0004","â¦");
str=str.replace("u0005","â£");
str=str.replace("u0006","â ");
str=str.replace("u0007","â¢");
str=str.replace("u0008","â");
str=str.replace("u0009","â");
str=str.replace("u000A","â");
str=str.replace("u000B","â");
str=str.replace("u000C","â");
str=str.replace("u000D","âª");
str=str.replace("u000E","â«");
str=str.replace("u000F","â¼");
str=str.replace("u0010","âº");
str=str.replace("u0011","â");
str=str.replace("u0012","â");
str=str.replace("u0013","â¼");
str=str.replace("u0014","¶");
str=str.replace("u0015","§");
str=str.replace("u0016","?");
str=str.replace("u0017","?");
str=str.replace("u0018","â");
str=str.replace("u0019","â");
str=str.replace("u001A","â");
str=str.replace("u001B","â");
str=str.replace("u001C","â");
str=str.replace("u001D","â");
str=str.replace("u001E","â²");
str=str.replace("u001F","â¼");
str=str.replace("u0020"," ");
str=str.replace("u0021","!");
str=str.replace("u0022","\"");
str=str.replace("u0023","#");
str=str.replace("u0024","$");
str=str.replace("u0025","%");
str=str.replace("u0026","&");
str=str.replace("u0027","'");
str=str.replace("u0028","(");
str=str.replace("u0029",")");
str=str.replace("u002A","*");
str=str.replace("u002B","+");
str=str.replace("u002C",",");
str=str.replace("u002D","-");
str=str.replace("u002E",".");
str=str.replace("u2026","â¦");
str=str.replace("u002F","/");
str=str.replace("u0030","0");
str=str.replace("u0031","1");
str=str.replace("u0032","2");
str=str.replace("u0033","3");
str=str.replace("u0034","4");
str=str.replace("u0035","5");
str=str.replace("u0036","6");
str=str.replace("u0037","7");
str=str.replace("u0038","8");
str=str.replace("u0039","9");
str=str.replace("u003A",":");
str=str.replace("u003B",";");
str=str.replace("u003C","<");
str=str.replace("u003D","=");
str=str.replace("u003E",">");
str=str.replace("u2264","â¤");
str=str.replace("u2265","â¥");
str=str.replace("u003F","?");
str=str.replace("u0040","@");
str=str.replace("u0041","A");
str=str.replace("u0042","B");
str=str.replace("u0043","C");
str=str.replace("u0044","D");
str=str.replace("u0045","E");
str=str.replace("u0046","F");
str=str.replace("u0047","G");
str=str.replace("u0048","H");
str=str.replace("u0049","I");
str=str.replace("u004A","J");
str=str.replace("u004B","K");
str=str.replace("u004C","L");
str=str.replace("u004D","M");
str=str.replace("u004E","N");
str=str.replace("u004F","O");
str=str.replace("u0050","P");
str=str.replace("u0051","Q");
str=str.replace("u0052","R");
str=str.replace("u0053","S");
str=str.replace("u0054","T");
str=str.replace("u0055","U");
str=str.replace("u0056","V");
str=str.replace("u0057","W");
str=str.replace("u0058","X");
str=str.replace("u0059","Y");
str=str.replace("u005A","Z");
str=str.replace("u005B","[");
str=str.replace("u005C","\\");
str=str.replace("u005D","]");
str=str.replace("u005E","^");
str=str.replace("u005F","_");
str=str.replace("u0060","`");
str=str.replace("u0061","a");
str=str.replace("u0062","b");
str=str.replace("u0063","c");
str=str.replace("u0064","d");
str=str.replace("u0065","e");
str=str.replace("u0066","f");
str=str.replace("u0067","g");
str=str.replace("u0068","h");
str=str.replace("u0069","i");
str=str.replace("u006A","j");
str=str.replace("u006B","k");
str=str.replace("u006C","l");
str=str.replace("u006D","m");
str=str.replace("u006E","n");
str=str.replace("u006F","o");
str=str.replace("u0070","p");
str=str.replace("u0071","q");
str=str.replace("u0072","r");
str=str.replace("u0073","s");
str=str.replace("u0074","t");
str=str.replace("u0075","u");
str=str.replace("u0076","v");
str=str.replace("u0077","w");
str=str.replace("u0078","x");
str=str.replace("u0079","y");
str=str.replace("u007A","z");
str=str.replace("u007B","{");
str=str.replace("u007C","|");
str=str.replace("u007D","}");
str=str.replace("u02DC","Ë");
str=str.replace("u007E","â¼");
str=str.replace("u007F","");
str=str.replace("u00A2","¢");
str=str.replace("u00A3","£");
str=str.replace("u00A4","¤");
str=str.replace("u20AC","â¬");
str=str.replace("u00A5","Â¥");
str=str.replace("u0026quot;","\"");
str=str.replace("u0026gt;",">");
str=str.replace("u0026lt;",">");
return str;
}

Javascript unicode Encoder

function simple_unicode_encode(str) {
str=str.replace("âº","u0001");
str=str.replace("â»","u0002");
str=str.replace("â¥","u0003");
str=str.replace("â¦","u0004");
str=str.replace("â£","u0005");
str=str.replace("â ","u0006");
str=str.replace("â¢","u0007");
str=str.replace("â","u0008");
str=str.replace("â","u0009");
str=str.replace("â","u000A");
str=str.replace("â","u000B");
str=str.replace("â","u000C");
str=str.replace("âª","u000D");
str=str.replace("â«","u000E");
str=str.replace("â¼","u000F");
str=str.replace("âº","u0010");
str=str.replace("â","u0011");
str=str.replace("â","u0012");
str=str.replace("â¼","u0013");
str=str.replace("¶","u0014");
str=str.replace("§","u0015");
str=str.replace("?","u0016");
str=str.replace("?","u0017");
str=str.replace("â","u0018");
str=str.replace("â","u0019");
str=str.replace("â","u001A");
str=str.replace("â","u001B");
str=str.replace("â","u001C");
str=str.replace("â","u001D");
str=str.replace("â²","u001E");
str=str.replace("â¼","u001F");
str=str.replace(" ","u0020");
str=str.replace("!","u0021");
str=str.replace("\"","u0022");
str=str.replace("#","u0023");
str=str.replace("$","u0024");
str=str.replace("%","u0025");
str=str.replace("&","u0026");
str=str.replace("'","u0027");
str=str.replace("(","u0028");
str=str.replace(")","u0029");
str=str.replace("*","u002A");
str=str.replace("+","u002B");
str=str.replace(",","u002C");
str=str.replace("-","u002D");
str=str.replace(".","u002E";
str=str.replace("â¦","u2026");
str=str.replace("/","u002F");
str=str.replace("0","u0030");
str=str.replace("1","u0031");
str=str.replace("2","u0032");
str=str.replace("3","u0033");
str=str.replace("4","u0034");
str=str.replace("5","u0035");
str=str.replace("6","u0036");
str=str.replace("7","u0037");
str=str.replace("8","u0038");
str=str.replace("9","u0039");
str=str.replace(":","u003A");
str=str.replace(";","u003B");
str=str.replace("<","u003C");
str=str.replace("=","u003D");
str=str.replace(">","u003E");
str=str.replace("â¤","u2264");
str=str.replace("â¥","u2265");
str=str.replace("?","u003F");
str=str.replace("@","u0040");
str=str.replace("A","u0041");
str=str.replace("B","u0042");
str=str.replace("C","u0043");
str=str.replace("D","u0044");
str=str.replace("E","u0045");
str=str.replace("F","u0046");
str=str.replace("G","u0047");
str=str.replace("H","u0048");
str=str.replace("I","u0049");
str=str.replace("J","u004A");
str=str.replace("K","u004B");
str=str.replace("L","u004C");
str=str.replace("M","u004D");
str=str.replace("N","u004E");
str=str.replace("O","u004F");
str=str.replace("P","u0050");
str=str.replace("Q","u0051");
str=str.replace("R","u0052");
str=str.replace("S","u0053");
str=str.replace("T","u0054");
str=str.replace("U","u0055");
str=str.replace("V","u0056");
str=str.replace("W","u0057");
str=str.replace("X","u0058");
str=str.replace("Y","u0059");
str=str.replace("Z","u005A");
str=str.replace("[","u005B");
str=str.replace("\\","u005C");
str=str.replace("]","u005D");
str=str.replace("^","u005E");
str=str.replace("_","u005F");
str=str.replace("`","u0060");
str=str.replace("a","u0061");
str=str.replace("b","u0062");
str=str.replace("c","u0063");
str=str.replace("d","u0064");
str=str.replace("e","u0065");
str=str.replace("f","u0066");
str=str.replace("g","u0067");
str=str.replace("h","u0068");
str=str.replace("i","u0069");
str=str.replace("j","u006A");
str=str.replace("k","u006B");
str=str.replace("l","u006C");
str=str.replace("m","u006D");
str=str.replace("n","u006E");
str=str.replace("o","u006F");
str=str.replace("p","u0070");
str=str.replace("q","u0071");
str=str.replace("r","u0072");
str=str.replace("s","u0073");
str=str.replace("t","u0074");
str=str.replace("u","u0075");
str=str.replace("v","u0076");
str=str.replace("w","u0077");
str=str.replace("x","u0078");
str=str.replace("y","u0079");
str=str.replace("z","u007A");
str=str.replace("{","u007B");
str=str.replace("|","u007C");
str=str.replace("}","u007D");
str=str.replace("Ë","u02DC");
str=str.replace("â¼","u007E");
str=str.replace(",")"u007F");
str=str.replace("¢","u00A2");
str=str.replace("£","u00A3");
str=str.replace("¤","u00A4");
str=str.replace("â¬","u20AC");
str=str.replace("Â¥","u00A5");
return str;
}

javascript string replace method would replace only the first occurance in the string. to replace all the occurances use the regular expression followed by "g".

ex: str=str.replace(/u0038/g,"8");

More by this Author


Comments

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working