Study/Java

[Class String] Encode/Decode Constructor, Method

momong'-' 2020. 7. 16. 12:31

Encoding (인코딩) Method

Modifier and Type Method and Description
byte[] getByte()
byte[] getByte(Charset charset)
void getByte(int srcBegin, int srcEnd, byte[] dst, int dstBegin)
byte[] getByte(String charsetName)

 


Decoding(디코딩) Method

Constructor and Description
String(byte[] bytes)
String(byte[] bytes, Charset charset)
String(byte[] bytes, int offset, int length)
String(byte[] bytes, int offset, int length, Charset charset)St
String(byte[] ascii, int hibyte, int offset, int count)
String(byte[] bytes, int offset, int length, String charsetName)
String(byte[] bytes, String charsetName)

 


Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.io.UnsupportedEncodingException;
 
public class StringTest {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String str = "안녕하세요";
        
        System.out.println("=======================");
        System.out.println("인코딩 전: " + str);
        System.out.println("=======================");
        String encodingKo = new String (str.getBytes("KSC5601"), "8859_1");
        System.out.println("인코딩: " + encodingKo);
        System.out.println("=======================");
        String decodingKo = new String (encodingKo.getBytes("8859_1"), "KSC5601");
        System.out.println("디코딩: " + decodingKo);
    }
}
 
cs
=======================
인코딩 전: 안녕하세요
=======================
인코딩: ¾È³çÇϼ¼¿ä
=======================
디코딩: 안녕하세요

 

 

 

참고