當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Java.lang.String.getByte()用法及代碼示例


java中的getbytes()函數用於將字符串轉換為字節序列並返回字節數組。此函數可以通過兩種方式實現。本文討論了這兩種方式。

  1. 語法1-public byte[] getBytes():此函數不帶參數,並使用默認字符集將字符串編碼為字節。
    // Java code to demonstrate the working of 
    // getByte() 
    public class GetByte { 
      
    public static void main(String args[]) 
        { 
            // Initializing String 
            String gfg = "ASTHA GFG"; 
      
            // Displaying string values before 
            // conversion 
            System.out.println("The String before conversion is : "); 
            System.out.println(gfg); 
      
            // converting the string into byte 
            // using getBytes ( converts into ASCII values ) 
            byte[] b = gfg.getBytes(); 
      
            // Displaying converted string after conversion 
            System.out.println("The String after conversion is : "); 
            for (int i = 0; i < b.length; i++) { 
                System.out.print(b[i]); 
            } 
        } 
    }

    輸出:

    The String before conversion is : 
    ASTHA GFG
    The String after conversion is : 
    658384726532717071
    
  2. 語法2:public byte [] getBytes(Charset charset):此實現接受字符集,根據字符集必須在轉換為字節時對其進行編碼。定義了許多字符集,下麵進行討論。
    • US-ASCII:Seven-bit ASCII,也稱為ISO646-US,也稱為Unicode字符集的基本拉丁語塊
    • ISO-8859-1:ISO拉丁字母1號,又名ISO-LATIN-1
    • UTF-8:Eight-bit UCS轉換格式
    • UTF-16BE:十六位UCS轉換格式,大端字節順序
    • UTF-16LE:十六位UCS轉換格式,低位字節序
    • UTF-16:十六位UCS轉換格式,由可選字節順序標記標識的字節順序。
    // Java code to demonstrate the working of 
    // getByte() using different character sets 
    import java.io.*; 
    public class GetBytecharset { 
      
    public static void main(String args[]) 
        { 
            // Initializing String 
            String gfg = new String("ASTHA GFG"); 
      
            // Displaying string values before 
            // conversion 
            System.out.println("The String before conversion is : "); 
            System.out.println(gfg); 
      
            try { 
      
                // converting the string into byte 
                // using getBytes ( converts into UTF-16 values ) 
                byte[] b = gfg.getBytes("UTF-16"); 
      
                // Displaying converted string after conversion 
                // into UTF-16 
                System.out.println("The String after conversion into UTF-16 is : "); 
                for (int i = 0; i < b.length; i++) { 
                    System.out.print(b[i]); 
                } 
      
                System.out.print("\n"); 
      
                // converting the string into byte 
                // using getBytes ( converts into UTF-16BE values ) 
                byte[] c = gfg.getBytes("UTF-16BE"); 
      
                // Displaying converted string after conversion 
                // into UTF-16BE 
                System.out.println("The String after conversion into UTF-16BE is : "); 
                for (int i = 0; i < c.length; i++) { 
                    System.out.print(c[i]); 
                } 
            } 
            catch (UnsupportedEncodingException g) { 
                System.out.println("Unsupported character set" + g); 
            } 
        } 
    }

    輸出:


    The String before conversion is : 
    ASTHA GFG
    The String after conversion into UTF-16 is : 
    -2-1065083084072065032071070071
    The String after conversion into UTF-16BE is : 
    065083084072065032071070071
    


相關用法


注:本文由純淨天空篩選整理自 Java.lang.String.getByte() in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。