1

Is there a java library to convert special characters into decimal equivalent?

example: input: "©™®" output: "& #169; & #8482; & #174;"(space after & is only for question purpose, if typed without a space decimal equivalent is converted to special character)

Thank you !

krb
  • 317
  • 3
  • 8

2 Answers2

3

This can be simply achieved with String.format(). The representations are simply the character value as decimal, padded to 4 characters and wrapped in &#;

The only tricky part is deciding which characters are "special". Here I've assumed not digit, not whitespace and not alpha...

StringBuilder output = new StringBuilder();
String input = "Foo bar ©™® baz";
for (char each : input.toCharArray()) {
    if (Character.isAlphabetic(each) || Character.isDigit(each) || Character.isWhitespace(each)) {
        output.append(each);
    } else {
        output.append(String.format("&#%04d;", (int) each));
    }
}
System.out.println(output.toString());
Adam
  • 35,919
  • 9
  • 100
  • 137
2

You just need to fetch the integer value of the character as mentioned in How do I get the decimal value of a unicode character in Java?.

As per Oracle Java doc

char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

Assuming your characters fall within the character range, you can just get the decimal equivalent of each character from your string.

    String text = "©™®";
    char[] cArr = text.toCharArray(); 
    for (char c : cArr)
    {
        int value = c; // get the decimal equivalent of the character
        String result = "& #" + value; // append to some format string
        System.out.println(result);
    }

Output:

& #169
& #8482
& #174
Community
  • 1
  • 1
Devendra Lattu
  • 2,732
  • 2
  • 18
  • 27