📜  Java中的 String.getByte() 方法

📅  最后修改于: 2022-05-13 01:55:16.145000             🧑  作者: Mango

Java中的 String.getByte() 方法

getBytes() 使用命名字符集将字符串编码为字节序列,并将结果存储到新的字节数组中。该函数可以通过两种方式实现。下面分别讨论这两种方式:

  • 获取字节()
  • getBytes(字符集字符集)

让我们讨论并实现第一个用例,如下所示:

获取字节()

此函数不接受任何参数,并使用默认字符集将字符串编码为字节。 Java中的getbytes()函数用于将字符串转换为字节序列并返回字节数组。

句法:

public byte[] getBytes()

示例 1:

Java
// Java Program to Demonstrate Working of getByte() Method
  
// Importing required classes
import java.util.*;
  
// Main class
// GetByte
public class GFG {
  
    // Main driver method
    public static void main(String args[])
    {
  
        // Declaring and initializing a string
        String gfg = "Geeks for Geeks";
  
        // Displaying string values before conversion
        System.out.println(
            "The String before conversion is : ");
        System.out.println(gfg);
  
        // Converting the string into byte
        // using getBytes() method
        byte[] b = gfg.getBytes();
  
        // Display message only
        System.out.println(
            "The String after conversion is : ");
  
        // Printing converted string after conversion
        for (int i = 0; i < b.length; i++) {
  
            System.out.print(b[i]);
        }
    }
}


Java
// Java code to Illustrate Working of getByte() Method
// using Different Character Sets
  
// Importing required classes
import java.io.*;
  
// Main class
public class GFG {
  
    // Main driver method
    public static void main(String args[])
    {
  
        // Declaring and initializinga string
        String gfg = new String("Geeks for Geeks");
  
        // Displaying string values before conversion
        System.out.println(
            "The String before conversion is : ");
        System.out.println(gfg);
  
        // Try block to check for exceptions
        try {
  
            // Converting the string into byte
            // using getBytes() method
            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 : ");
  
            // Iterating over the string
            for (int i = 0; i < b.length; i++) {
                System.out.print(b[i]);
            }
  
            System.out.print("\n");
  
            // Converting the above string into byte
            // using getBytes() method
            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 block to handle exceptions
        catch (UnsupportedEncodingException g) {
  
            // Display message when exceptions occurs
            System.out.println("Unsupported character set"
                               + g);
        }
    }
}


输出
The String before conversion is : 
Geeks for Geeks
The String after conversion is : 
71101101107115321021111143271101101107115

getBytes(字符集字符集)

现在让我们实现接受字符集,根据在转换为字节时必须对哪个字符串进行编码。定义了许多字符集,并在下面讨论。

句法:

public byte[] getBytes(Charset charset);
  • US-ASCII:七位 ASCII,又名 ISO646-US,又名 Unicode字符集的基本拉丁语块
  • ISO-8859-1: ISO 拉丁字母 No. 1,又名 ISO-LATIN-1
  • UTF-8:八位 UCS 转换格式
  • UTF-16BE:十六位 UCS 转换格式,大端字节序
  • UTF-16LE:十六位 UCS 转换格式,小端字节序
  • UTF-16: 16 位 UCS 转换格式,由可选字节顺序标记标识的字节顺序。

例子:

Java

// Java code to Illustrate Working of getByte() Method
// using Different Character Sets
  
// Importing required classes
import java.io.*;
  
// Main class
public class GFG {
  
    // Main driver method
    public static void main(String args[])
    {
  
        // Declaring and initializinga string
        String gfg = new String("Geeks for Geeks");
  
        // Displaying string values before conversion
        System.out.println(
            "The String before conversion is : ");
        System.out.println(gfg);
  
        // Try block to check for exceptions
        try {
  
            // Converting the string into byte
            // using getBytes() method
            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 : ");
  
            // Iterating over the string
            for (int i = 0; i < b.length; i++) {
                System.out.print(b[i]);
            }
  
            System.out.print("\n");
  
            // Converting the above string into byte
            // using getBytes() method
            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 block to handle exceptions
        catch (UnsupportedEncodingException g) {
  
            // Display message when exceptions occurs
            System.out.println("Unsupported character set"
                               + g);
        }
    }
}
输出
The String before conversion is : 
Geeks for Geeks
The String after conversion into UTF-16 is : 
-2-107101010101010701150320102011101140320710101010101070115
The String after conversion into UTF-16BE is : 
07101010101010701150320102011101140320710101010101070115