How to Convert Utf-8 to Us-Ascii in Java

Getting UTF-8 encoded from US-ASCII encoded string

You can't. You lost information by converting to US-ASCII. You can't get back what was lost.

How to convert UTF-8 to US-Ascii in Java

The uni2ascii program is written in C, but you could probably convert it to Java with little effort. It contains a large table of approximations (implicitly, in the switch-case statements).

Be aware that there are no universally accepted approximations: Germans want you to replace Ä by AE, Finns and Swedes prefer just A. Your example of Å isn't obvious either: Swedes would probably just drop the ring and use A, but Danes and Norwegians might like the historically more correct AA better.

Getting UTF8 charset instead of ASCII in java

Remove the System.setProperty lines, they're useless and don't do what you think.

Since you didn't specify the encoding in the InputStreamReader constructor, the platform default is used, which in your case seems to be UTF-8. Add "US-ASCII" as a parameter to InputStreamReader.

InputStreamReader does not guess the encoding. It uses the platform default unless you specify differently, and you should always explicitly specify the encoding to make sure that the program works the same on different platforms.

Convert character to ASCII numeric value in java

Very simple. Just cast your char as an int.

char character = 'a';    
int ascii = (int) character;

In your case, you need to get the specific Character from the String first and then cast it.

char character = name.charAt(0); // This gives the character 'a'
int ascii = (int) character; // ascii is now 97.

Though cast is not required explicitly, but its improves readability.

int ascii = character; // Even this will do the trick.


Related Topics



Leave a reply



Submit