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
How to Improve the Performance of G.Drawimage() Method for Resizing Images
Why Dispatcherservlet Creates Another Application Context
How to Do N-Level Nested Loops in Java
Annotations from Javax.Validation.Constraints Not Working
How to Configure Hibernate Config File for SQL Server
Console Application with Java and Gradle
How to Configure Log4J with a Properties File
How to Format a Number 0..9 to Display with 2 Digits (It's Not a Date)
How to Hide a Jframe in System Tray of Taskbar
Read Error Response Body in Java
Fastest Way to Iterate an Array in Java: Loop Variable VS Enhanced for Statement
Why Did Servlet.Service() for Servlet Jsp Throw This Exception
How to Simulate a Real Mouse Click Using Java
Which Loop Has Better Performance? Why
What Is the Shortest Way to Pretty Print a Org.W3C.Dom.Document to Stdout