Java的中文處理 - String轉換bytes和bytes轉String


import java.io.UnsupportedEncodingException;

class DataProcess {
    public static byte[] stringToBytes(String str) {
        try {
            // 使用指定的字符集將此字符串編碼為byte序列並存到一個byte數組中
            return str.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String bytesToString(byte[] bs) {
        try {
            // 通過指定的字符集解碼指定的byte數組並構造一個新的字符串
            return new String(bs, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}

 

參考:

1)java中怎么從byte字節流轉換為中文

2)中文轉unicode,中文轉bytes,unicode轉bytes java實現


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM