Knowledge/Java Javascript의 unescape, Java(JSP)에서 사용하기 고추장불고기 2008. 6. 9. 10:37 역시나 개인적인 필요로 의해......입력값중 %RE 등등... 헥스값이 아닌 경우엔 입력값 그대로 출력함. (Language : java) public static String unescape(String inp) { String rtnStr = new String(); char [] arrInp = inp.toCharArray(); int i; for(i=0;i<arrInp.length;i++) { if(arrInp[i] == '%') { String hex; if(arrInp[i+1] == 'u') { //유니코드. hex = inp.substring(i+2, i+6); i += 5; } else { //ascii hex = inp.substring(i+1, i+3); i += 2; } try{ rtnStr += new String(Character.toChars(Integer.parseInt(hex, 16))); } catch(NumberFormatException e) { rtnStr += "%"; i -= (hex.length()>2 ? 5 : 2); } } else { rtnStr += arrInp[i]; } } return rtnStr; } Character.toChars()가 1.5부터 지원,1.4 환경에서 사용가능한 unescape. (Language : java) public static String unescape(String inp) { String rtnStr = new String(); char [] arrInp = inp.toCharArray(); int i; for(i=0;i<arrInp.length;i++) { if(arrInp[i] == '%') { String hex; String str; if(arrInp[i+1] == 'u') { //유니코드. hex = inp.substring(i+2, i+6); i += 5; } else { //ascii hex = inp.substring(i+1, i+3); i += 2; } try{ byte [] b; if(hex.length() == 2) { b = new byte[1]; b[0] = (byte)(Integer.parseInt(hex, 16)); str = new String(b, "ASCII"); } else { b = new byte[2]; b[0] = (byte)(Integer.parseInt(hex.substring(0,2), 16)); b[1] = (byte)(Integer.parseInt(hex.substring(2,4), 16)); str = new String(b, "UTF-16"); } rtnStr += str; } catch(NumberFormatException e) { e.printStackTrace(); rtnStr += "%"; i -= (hex.length()>2 ? 5 : 2); } catch(Exception e) { } } else { rtnStr += arrInp[i]; } } return rtnStr; }