JAVA Internalization Tutorial
Selected Reading
- Java I18N - Discussion
- Java I18N - Useful Resources
- Java I18N - Quick Guide
- JAVA I18N - From Reader and To Writer Conversion
- JAVA I18N - From and To String Conversion
- JAVA I18N - UTC
- JAVA I18N - Date Format Patterns
- JAVA I18N - DateFormatSymbols Class
- JAVA I18N - Formatting Date
- JAVA I18N - SimpleDateFormat Class
- JAVA I18N - Formatting Date and Time
- JAVA I18N - Formatting Time
- JAVA I18N - Formatting Dates
- JAVA I18N - DateFormat Class
- JAVA I18N - Grouping Digits
- JAVA I18N - DecimalFormatSymbols Class
- JAVA I18N - Locale Specific DecimalFormat
- JAVA I18N - Formatting Patterns
- JAVA I18N - DecimalFormat Class
- JAVA I18N - Parsing Numbers
- JAVA I18N - Set Rounding Mode
- JAVA I18N - Set Min/Max Precision
- JAVA I18N - Format Percentages
- JAVA I18N - Format Currencies
- JAVA I18N - NumberFormat Class
- JAVA I18N - ResourceBundle Class
- JAVA I18N - Display Language
- JAVA I18N - Locale Details
- JAVA I18N - Locale Class
- JAVA I18N - Environment Setup
- JAVA I18N - Overview
- JAVA I18N - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
JAVA I18N - From and To String Conversion
Java Internapzation - Unicode Conversion from/to String
In java, text is internally stored in Unicode format. If input/output is in differnt format then conversion is required.
Conversion
Following example will showcase conversion of a Unicode String to UTF8 byte[] and UTF8 byte[] to Unicode byte[].
IOTester.java
import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.text.ParseException; pubpc class I18NTester { pubpc static void main(String[] args) throws ParseException, UnsupportedEncodingException { String unicodeString = "u00C6u00D8u00C5" ; //convert Unicode to UTF8 format byte[] utf8Bytes = unicodeString.getBytes(Charset.forName("UTF-8")); printBytes(utf8Bytes, "UTF 8 Bytes"); //convert UTF8 format to Unicode String converted = new String(utf8Bytes, "UTF8"); byte[] unicodeBytes = converted.getBytes(); printBytes(unicodeBytes, "Unicode Bytes"); } pubpc static void printBytes(byte[] array, String name) { for (int k = 0; k < array.length; k++) { System.out.println(name + "[" + k + "] = " + array[k]); } } }
Output
It will print the following result.
UTF 8 Bytes[0] = -61 UTF 8 Bytes[1] = -122 UTF 8 Bytes[2] = -61 UTF 8 Bytes[3] = -104 UTF 8 Bytes[4] = -61 UTF 8 Bytes[5] = -123 Unicode Bytes[0] = -58 Unicode Bytes[1] = -40 Unicode Bytes[2] = -59