在 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
相關用法
- Java String compareToIgnoreCase()用法及代碼示例
- Java String compareTo()用法及代碼示例
- Java String split()用法及代碼示例
- Java String length()用法及代碼示例
- Java String replace()用法及代碼示例
- Java String replaceAll()用法及代碼示例
- Java String substring()用法及代碼示例
- Java String equals()用法及代碼示例
- Java String equalsIgnoreCase()用法及代碼示例
- Java String contains()用法及代碼示例
- Java String indexOf()用法及代碼示例
- Java String trim()用法及代碼示例
- Java String charAt()用法及代碼示例
- Java String toLowerCase()用法及代碼示例
- Java String concat()用法及代碼示例
- Java String valueOf()用法及代碼示例
- Java String matches()用法及代碼示例
- Java String startsWith()用法及代碼示例
- Java String endsWith()用法及代碼示例
- Java String isEmpty()用法及代碼示例
- Java String intern()用法及代碼示例
- Java String getBytes()用法及代碼示例
- Java String contentEquals()用法及代碼示例
- Java String hashCode()用法及代碼示例
- Java String join()用法及代碼示例
注:本文由純淨天空篩選整理自佚名大神的英文原創作品 String.getByte() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。