logo

Java String getBytes()

Java 文字列クラス getBytes() このメソッドは、文字列をバイトのシーケンスにエンコードし、それをバイトの配列に保持します。

サイン

getBytes() メソッドには 3 つのバリエーションがあります。 string getBytes() メソッドのシグネチャまたは構文を以下に示します。

 public byte[] getBytes() public byte[] getBytes(Charset charset) public byte[] getBytes(String charsetName)throws UnsupportedEncodingException 

パラメーター

charset / charsetName - メソッドがサポートする文字セットの名前。

戻り値

バイトのシーケンス。

例外スロー

UnsupportedEncodingException: これは、指定された文字セットがメソッドでサポートされていない場合にスローされます。

内部実装

 public byte[] getBytes() { return StringCoding.encode(value, 0, value.length); } 

StringクラスのgetBytes()メソッドの例

パラメーターのない getBytes() メソッドは、プラットフォームのデフォルトの文字セット (UTF - 8) を使用して文字列をエンコードします。次の 2 つの例は、同じことを示しています。

サイラ・バヌ俳優

ファイル名: StringGetBytesExample.java

 public class StringGetBytesExample{ public static void main(String args[]){ String s1=&apos;ABCDEFG&apos;; byte[] barr=s1.getBytes(); for(int i=0;i <barr.length;i++){ system.out.println(barr[i]); } }} < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 65 66 67 68 69 70 71 </pre> <h2>Java String class getBytes() Method Example 2</h2> <p> <strong>FileName:</strong> StringGetBytesExample2.java</p> <p>The method returns a byte array that again can be passed to the String constructor to get String.</p> <pre> public class StringGetBytesExample2 { public static void main(String[] args) { String s1 = &apos;ABCDEFG&apos;; byte[] barr = s1.getBytes(); for(int i=0;i <barr.length;i++){ system.out.println(barr[i]); } getting string back s2="new" string(barr); system.out.println(s2); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 65 66 67 68 69 70 71 ABCDEFG </pre> <h2>Java String class getBytes() Method Example 3</h2> <p>The following example shows the encoding into a different charset.</p> <p> <strong>FileName:</strong> StringGetBytesExample3.java</p> <pre> // Import statement import java.io.*; public class StringGetBytesExample3 { // main method public static void main(String argvs[]) { // input string String str = &apos;Welcome to JavaTpoint.&apos;; System.out.println(&apos;The input String is : &apos;); System.out.println(str + &apos;
&apos;); // inside try block encoding is // being done using different charsets try { 16 - bit UCS Transformation format byte[] byteArr = str.getBytes(&apos;UTF-16&apos;); System.out.println(&apos;After converted into UTF-16 the String is : &apos;); for (int j = 0; j <bytearr.length; 16 j++) { system.out.print(bytearr[j]); } system.out.println('
'); big endian byte order, - bit ucs transformation format byte[] bytearr1="str.getBytes(&apos;UTF-16BE&apos;);" system.out.println('after converted into utf-16be the string is : '); for (int j="0;" < bytearr1.length; system.out.print(bytearr1[j]); iso latin alphabet bytearr2="str.getBytes(&apos;ISO-8859-1&apos;);" iso-8859-1 bytearr2.length; system.out.print(bytearr2[j]); little bytearr3="str.getBytes(&apos;UTF-16LE&apos;);" utf-16le bytearr3.length; system.out.print(bytearr3[j]); catch (unsupportedencodingexception g) system.out.println('unsupported character set' + g); pre> <p> <strong>Output:</strong> </p> <pre> The input String is : Welcome to JavaTpoint. After converted into UTF-16 the String is : -2-10870101010809901110109010103201160111032074097011809708401120111010501100116046 After converted into UTF-16BE the String is : 0870101010809901110109010103201160111032074097011809708401120111010501100116046 After converted into ISO-8859-1 the String is : 871011089911110910132116111327497118978411211110511011646 After converted into UTF-16LE the String is : 8701010108099011101090101032011601110320740970118097084011201110105011001160460 </pre> <h2>Java String class getBytes() Method Example 4</h2> <p>The following example shows when the charset is not supported by the getBytes() method, UnsupportedEncodingException is thrown.</p> <p> <strong>FileName:</strong> StringGetBytesExample4.java</p> <pre> public class StringGetBytesExample4 { // main method public static void main(String argvs[]) { // input string String str = &apos;Welcome to JavaTpoint.&apos;; System.out.println(&apos;The input String is : &apos;); System.out.println(str + &apos;
&apos;); // encoding into UTF - 17 byte[] byteArr = str.getBytes(&apos;UTF-17&apos;); System.out.println(&apos;After converted into UTF-17 the String is : &apos;); for (int j = 0; j <bytearr.length; j++) { system.out.print(bytearr[j]); } < pre> <p> <strong>Output:</strong> </p> <pre> /StringGetBytesExample4.java:11: error: unreported exception UnsupportedEncodingException; must be caught or declared to be thrown byte[] byteArr = str.getBytes(&apos;UTF-17&apos;); ^ 1 error </pre> <hr></bytearr.length;></pre></bytearr.length;></pre></barr.length;i++){></pre></barr.length;i++){>

Java String クラス getBytes() メソッドの例 2

ファイル名: StringGetBytesExample2.java

このメソッドは、再び String コンストラクターに渡して String を取得できるバイト配列を返します。

 public class StringGetBytesExample2 { public static void main(String[] args) { String s1 = &apos;ABCDEFG&apos;; byte[] barr = s1.getBytes(); for(int i=0;i <barr.length;i++){ system.out.println(barr[i]); } getting string back s2="new" string(barr); system.out.println(s2); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 65 66 67 68 69 70 71 ABCDEFG </pre> <h2>Java String class getBytes() Method Example 3</h2> <p>The following example shows the encoding into a different charset.</p> <p> <strong>FileName:</strong> StringGetBytesExample3.java</p> <pre> // Import statement import java.io.*; public class StringGetBytesExample3 { // main method public static void main(String argvs[]) { // input string String str = &apos;Welcome to JavaTpoint.&apos;; System.out.println(&apos;The input String is : &apos;); System.out.println(str + &apos;
&apos;); // inside try block encoding is // being done using different charsets try { 16 - bit UCS Transformation format byte[] byteArr = str.getBytes(&apos;UTF-16&apos;); System.out.println(&apos;After converted into UTF-16 the String is : &apos;); for (int j = 0; j <bytearr.length; 16 j++) { system.out.print(bytearr[j]); } system.out.println(\'
\'); big endian byte order, - bit ucs transformation format byte[] bytearr1="str.getBytes(&apos;UTF-16BE&apos;);" system.out.println(\'after converted into utf-16be the string is : \'); for (int j="0;" < bytearr1.length; system.out.print(bytearr1[j]); iso latin alphabet bytearr2="str.getBytes(&apos;ISO-8859-1&apos;);" iso-8859-1 bytearr2.length; system.out.print(bytearr2[j]); little bytearr3="str.getBytes(&apos;UTF-16LE&apos;);" utf-16le bytearr3.length; system.out.print(bytearr3[j]); catch (unsupportedencodingexception g) system.out.println(\'unsupported character set\' + g); pre> <p> <strong>Output:</strong> </p> <pre> The input String is : Welcome to JavaTpoint. After converted into UTF-16 the String is : -2-10870101010809901110109010103201160111032074097011809708401120111010501100116046 After converted into UTF-16BE the String is : 0870101010809901110109010103201160111032074097011809708401120111010501100116046 After converted into ISO-8859-1 the String is : 871011089911110910132116111327497118978411211110511011646 After converted into UTF-16LE the String is : 8701010108099011101090101032011601110320740970118097084011201110105011001160460 </pre> <h2>Java String class getBytes() Method Example 4</h2> <p>The following example shows when the charset is not supported by the getBytes() method, UnsupportedEncodingException is thrown.</p> <p> <strong>FileName:</strong> StringGetBytesExample4.java</p> <pre> public class StringGetBytesExample4 { // main method public static void main(String argvs[]) { // input string String str = &apos;Welcome to JavaTpoint.&apos;; System.out.println(&apos;The input String is : &apos;); System.out.println(str + &apos;
&apos;); // encoding into UTF - 17 byte[] byteArr = str.getBytes(&apos;UTF-17&apos;); System.out.println(&apos;After converted into UTF-17 the String is : &apos;); for (int j = 0; j <bytearr.length; j++) { system.out.print(bytearr[j]); } < pre> <p> <strong>Output:</strong> </p> <pre> /StringGetBytesExample4.java:11: error: unreported exception UnsupportedEncodingException; must be caught or declared to be thrown byte[] byteArr = str.getBytes(&apos;UTF-17&apos;); ^ 1 error </pre> <hr></bytearr.length;></pre></bytearr.length;></pre></barr.length;i++){>

Java String クラス getBytes() メソッドの例 3

次の例は、別の文字セットへのエンコードを示しています。

ファイル名: StringGetBytesExample3.java

 // Import statement import java.io.*; public class StringGetBytesExample3 { // main method public static void main(String argvs[]) { // input string String str = &apos;Welcome to JavaTpoint.&apos;; System.out.println(&apos;The input String is : &apos;); System.out.println(str + &apos;
&apos;); // inside try block encoding is // being done using different charsets try { 16 - bit UCS Transformation format byte[] byteArr = str.getBytes(&apos;UTF-16&apos;); System.out.println(&apos;After converted into UTF-16 the String is : &apos;); for (int j = 0; j <bytearr.length; 16 j++) { system.out.print(bytearr[j]); } system.out.println(\'
\'); big endian byte order, - bit ucs transformation format byte[] bytearr1="str.getBytes(&apos;UTF-16BE&apos;);" system.out.println(\'after converted into utf-16be the string is : \'); for (int j="0;" < bytearr1.length; system.out.print(bytearr1[j]); iso latin alphabet bytearr2="str.getBytes(&apos;ISO-8859-1&apos;);" iso-8859-1 bytearr2.length; system.out.print(bytearr2[j]); little bytearr3="str.getBytes(&apos;UTF-16LE&apos;);" utf-16le bytearr3.length; system.out.print(bytearr3[j]); catch (unsupportedencodingexception g) system.out.println(\'unsupported character set\' + g); pre> <p> <strong>Output:</strong> </p> <pre> The input String is : Welcome to JavaTpoint. After converted into UTF-16 the String is : -2-10870101010809901110109010103201160111032074097011809708401120111010501100116046 After converted into UTF-16BE the String is : 0870101010809901110109010103201160111032074097011809708401120111010501100116046 After converted into ISO-8859-1 the String is : 871011089911110910132116111327497118978411211110511011646 After converted into UTF-16LE the String is : 8701010108099011101090101032011601110320740970118097084011201110105011001160460 </pre> <h2>Java String class getBytes() Method Example 4</h2> <p>The following example shows when the charset is not supported by the getBytes() method, UnsupportedEncodingException is thrown.</p> <p> <strong>FileName:</strong> StringGetBytesExample4.java</p> <pre> public class StringGetBytesExample4 { // main method public static void main(String argvs[]) { // input string String str = &apos;Welcome to JavaTpoint.&apos;; System.out.println(&apos;The input String is : &apos;); System.out.println(str + &apos;
&apos;); // encoding into UTF - 17 byte[] byteArr = str.getBytes(&apos;UTF-17&apos;); System.out.println(&apos;After converted into UTF-17 the String is : &apos;); for (int j = 0; j <bytearr.length; j++) { system.out.print(bytearr[j]); } < pre> <p> <strong>Output:</strong> </p> <pre> /StringGetBytesExample4.java:11: error: unreported exception UnsupportedEncodingException; must be caught or declared to be thrown byte[] byteArr = str.getBytes(&apos;UTF-17&apos;); ^ 1 error </pre> <hr></bytearr.length;></pre></bytearr.length;>

Java String クラス getBytes() メソッドの例 4

次の例は、文字セットが getBytes() メソッドでサポートされていない場合に UnsupportedEncodingException がスローされることを示しています。

インターネットブラウザの設定

ファイル名: StringGetBytesExample4.java

 public class StringGetBytesExample4 { // main method public static void main(String argvs[]) { // input string String str = &apos;Welcome to JavaTpoint.&apos;; System.out.println(&apos;The input String is : &apos;); System.out.println(str + &apos;
&apos;); // encoding into UTF - 17 byte[] byteArr = str.getBytes(&apos;UTF-17&apos;); System.out.println(&apos;After converted into UTF-17 the String is : &apos;); for (int j = 0; j <bytearr.length; j++) { system.out.print(bytearr[j]); } < pre> <p> <strong>Output:</strong> </p> <pre> /StringGetBytesExample4.java:11: error: unreported exception UnsupportedEncodingException; must be caught or declared to be thrown byte[] byteArr = str.getBytes(&apos;UTF-17&apos;); ^ 1 error </pre> <hr></bytearr.length;>