当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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


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

  • getBytes()
  • getBytes(Charset charset)

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

Java 字符串 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]);
        }
    }
}
输出
The String before conversion is : 
Geeks for Geeks
The String after conversion is : 
71101101107115321021111143271101101107115

Java String getBytes(Charset 字符集)

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

用法

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:16 位 UCS 转换格式,big-endian 字节顺序
  • UTF-16LE:16 位 UCS 转换格式,little-endian 字节顺序
  • 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


相关用法


注:本文由纯净天空筛选整理自佚名大神的英文原创作品 String.getByte() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。