HuyPV
Saturday, January 29, 2011
Input:
Tiếng Việt có dấu, đỡ đi em
Output:
Ti\u1ebfng Vi\u1ec7t c\u00f3 d\u1ea5u, \u0111\u1ee1 \u0111i em!
JavaScript function:
function dec2hex(i)
{
var result = "0000";
if (i >= 0 && i <= 15) { result = "\\u000" + i.toString(16); }
else if (i >= 16 && i <= 255) { result = "\\u00" + i.toString(16); }
else if (i >= 256 && i <= 4095) { result = "\\u0" + i.toString(16); }
else if (i >= 4096 && i <= 65535) { result = "\\u" + i.toString(16); }
return result;
}
x = prompt("String", "...");
if (x != null) {
l = x.length;
y = '';
for (i = 0; i < l; i++) {
c = x.charCodeAt(i);
if (c > 128)
y = y + dec2hex(c);
else
y = y+x.charAt(i);
}
document.write(y);
document.close();
Java function:
...
isr = new InputStreamReader(is,"UTF8");
buffer = new StringBuffer();
int ch;
while ((ch = isr.read()) > -1) {
if (ch < 128) {
System.out.print((char) ch);
} else {
System.out.print("\\u" + str_pad(java.lang.Integer.toHexString(ch), 4, '0'));
}
buffer.append((char)ch);
}
...
public static String str_pad(String str, int size, char padChar) {
StringBuffer padded = new StringBuffer(str);
while (padded.length() < size) {
padded.insert(0, padChar);
}
return padded.toString();
}
Title:
Unicode char - hex code
Description:
Input: Tiếng Việt có dấu, đỡ đi em Output: Ti\u1ebfng Vi\u1ec7t c\u00f3 d\u1ea5u, \u0111\u1ee1 \u0111i em! JavaScript function: func...
...
Rating:
4