Java 17 Recipes


-6. Converting Byte Arrays to and from Strings


Download 3.2 Mb.
Pdf ko'rish
bet211/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   207   208   209   210   211   212   213   214   ...   245
Bog'liq
Java 17 Recipes

11-6. Converting Byte Arrays to and from Strings
 Problem
You need to convert characters in a byte array from a legacy character set encoding to a 
Unicode string.
 Solution
Convert legacy character encodings from a byte array to a Unicode string using the 
String class. The following code snippet from the org.java17recipes.chapter11 
.recipe11_06.Recipe11_6 class demonstrates how to convert a legacy Shift-JIS encoded 
byte array to a string. Later in this same example, the code demonstrates how to convert 
Unicode back to the Shift-JIS byte array.
Chapter 11 UniCode, internationalization, and CUrrenCy Codes 


416
public static void main(String[] args) {
Recipe11_6 app = new Recipe11_6();
app.run();
}
public void run() {
byte[] legacySJIS = {(byte)0x82,(byte)0xB1,(byte)0x82,(byte)0xF1,
(byte)0x82,(byte)0xC9,(byte)0x82,(byte)0xBF,
(byte)0x82,(byte)0xCD,(byte)0x81,(byte)0x41,
(byte)0x90,(byte)0xA2,(byte)0x8A,(byte)0x45,
(byte)0x81,(byte)0x49};
// Convert a byte[] to a String
Charset cs =Charset.forName("SJIS");
String greeting = new String(legacySJIS, cs);
System.out.printf("Greeting: %s\n", greeting);
This code prints out the converted text, which is “Hello, world!” in Japanese.
Greeting: こんにちは、世界!
Use the getBytes() method to convert characters from a string to a byte array. 
Building on the previous code, convert back to the original encoding with the following 
code and compare the results.
// Convert a String to a byte[]
byte[] toSJIS = greeting.getBytes(cs);
// Confirm that the original array and newly converted array are same
Boolean same = false;
if (legacySJIS.length == toSJIS.length) {
for (int x=0; x< legacySJIS.length; x++) {
if(legacySJIS[x] != toSJIS[x]) break;
}
same = true;
}
System.out.printf("Same: %s\n", same.toString());
Chapter 11 UniCode, internationalization, and CUrrenCy Codes 


417
As expected, the output indicates that the round-trip conversion back to the legacy 
encoding was successful. The original byte array and the converted byte array contain 
the same bytes.
Same: true

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   207   208   209   210   211   212   213   214   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling