krb
krb

Reputation: 347

Convert special characters into decimal equivalents in java

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 !

Upvotes: 1

Views: 809

Answers (2)

Adam
Adam

Reputation: 36743

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());

Upvotes: 3

Devendra Lattu
Devendra Lattu

Reputation: 2812

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

Upvotes: 2

Related Questions