22 July 2010

Print unicode values of non-ascii characters


class GetUnicode {

static final char digits[] = {'0', '1', '2', '3','4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

public static void main(String[] args) {
String name = "محمد هويدي";
for (char ch : name.toCharArray())
if (Character.isWhitespace(ch))
System.out.print(ch);
else
System.out.print("\\u" + getHex(ch));
}
static String getHex(int ch) {
char tmp[] = new char[32];
int i, j, count =0;

for (i=0; i<32; i++)
tmp[i] = '0';
while (ch>0)
{
int t = ch&0xf;
ch>>=4;
tmp[count++] = digits[t];
}
char ret[] = new char[count +=(4-count&3)]; // make ret size to be multiple of four

for (i=count-1, j=0; i>=0; i--, j++)
ret[j] = tmp[i];

tmp = null;
return new String(ret);
}
}
Post a Comment