JavaでString と byte配列のとの相互変換

2008/10/14 18:35Update
TAGS: 文字列 | byte[] | byte配列

JavaでStringからbyte[]に、byte[]からStringに変換する方法について学びます。

import java.io.UnsupportedEncodingException;

public class TestString {
    public static void main(String []args) {
        String str = "abcあいうえお";
       
        try {
            //String -> byte[]
            byte [] bytes = str.getBytes("UTF-8");  //String.getBytes();    or      String.getBytes(encoding);
            
            //byte [] -> String
            String xx = new String(bytes, "UTF-8"); // 
            System.out.println(xx);
        } catch (UnsupportedEncodingException e) {
            // TODO 自動生成された catch ブロック
            e.printStackTrace();
        }
    }
}


String仕様(API)

有关作者
Syboos.jp編集長AJavaやオープンソース情報の執筆、Webサイトの開発や運営全般の業務に携わる。

Sponsored Link


Comments

用户名 (required)

Email (will not be published) (required)

URL

Evaluation