ใน Java วิธีการแปลง Byte [] Array To String และ String เป็น Byte []?
เผยแพร่แล้ว: 2019-12-26![ใน Java วิธีการแปลง Byte [] Array To String และ String เป็น Byte []?](/uploads/article/539/B3oJoW1JxaiAtSnk.png)
วิธีการแปลง Byte [] Array เป็น String ใน Java? วิธีแปลง UTF-8 ไบต์ [] เป็นสตริง? แปลง Java Byte Array เป็น String เป็น Byte Array
String เก็บ textual data
และสำหรับการจัดเก็บ binary data
คุณจะต้อง byte[] ในสถานการณ์ในอุดมคติ คุณจะต้องหลีกเลี่ยงการใช้สิ่งเดียวกันนี้ในบิลด์ที่พร้อมสำหรับการผลิตของคุณ นี่คือรหัส ในกรณีที่คุณจำเป็นต้องทำการแปลงในแอปพลิเคชันของคุณจาก String เป็น byte[] และในทางกลับกัน
มีหลายวิธีที่คุณสามารถแปลง byte[] เป็น String และ String เป็น byte[]
เราจะจัดการ String และ byte[] 5 different ways
- แปลงสตริงเป็น byte[] Array โดยใช้ getBytes()
- แปลงสตริงเป็นไบต์[]อาร์เรย์โดยใช้การเข้ารหัส UTF_8
- เข้ารหัสและถอดรหัสเป็นสตริงโดยใช้การเข้ารหัส Base64
- แปลง Byte[] เป็น String โดยใช้สตริงใหม่ ()
- แปลง Byte[] เป็น String โดยใช้การเข้ารหัส UTF_8
ฟังก์ชัน toString()
บนวัตถุ String จะไม่ส่งคืนสตริงจริง แต่จะมีเพียง HashValue เท่านั้น ค้นหาความคิดเห็นทั้งหมดที่กล่าวถึงในโปรแกรม Java ด้านล่าง
รหัสจาวา:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
package crunchify . com . tutorials ; import java . io . UnsupportedEncodingException ; import java . nio . charset . StandardCharsets ; import java . util . Arrays ; import java . util . Base64 ; /** * @author Crunchify.com * In Java How to convert Byte[] Array To String and String to Byte[]? * Version: 1.2 */ public class CrunchifyByteArrayToString { public static void main ( String [ ] args ) throws UnsupportedEncodingException { // Method-1: Convert String to byte[] Array using getBytes() String crunchifyString = "Crunchify Example on Byte[] to String..." ; // getBytes() encodes this String into a sequence of bytes using the platform's // default charset, storing the result into a new byte array. byte [ ] crunchifyByte = crunchifyString . getBytes ( ) ; System . out . println ( "crunchifyString : " + crunchifyString ) ; // toString() Returns a string representation of the contents of the specified array. // The string representation consists of a list of the array's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (a comma followed by a space). // Elements are converted to strings as by String.valueOf(byte). Returns "null" if a is null. System . out . println ( "crunchifyByte : " + Arrays . toString ( crunchifyByte ) ) ; // Method-2: Convert String to byte[] Array using UTF_8 encoding String crunchifyString2 = "This is test for converting String to byte...!" ; byte [ ] crunchifyByte2 = crunchifyString2 . getBytes ( StandardCharsets . UTF_8 ) ; System . out . println ( "\ncrunchifyString2 : " + crunchifyString2 ) ; System . out . println ( "crunchifyByte2 : " + Arrays . toString ( crunchifyByte2 ) ) ; // Method-3: Encode and Decode to String using Base64 encoding // encodeToString() Encodes the specified byte array into a String using the Base64 encoding scheme. // This method first encodes all input bytes into a base64 encoded byte array and then constructs // a new String by using the encoded byte array and the ISO-8859-1 charset. String crunchifyEncodedValue = Base64 . getEncoder ( ) . encodeToString ( crunchifyByte2 ) ; System . out . println ( "crunchifyByte2 encoded value : " + crunchifyEncodedValue ) ; // decode() Decodes a Base64 encoded String into a newly-allocated byte array using the Base64 encoding scheme. // An invocation of this method has exactly the same effect as invoking decode(src.getBytes(StandardCharsets.ISO_8859_1)) byte [ ] crunchifyByte3 = Base64 . getDecoder ( ) . decode ( crunchifyEncodedValue ) ; System . out . println ( "crunchifyByte2 decoded value : " + Arrays . toString ( crunchifyByte3 ) ) ; // Method-4: Convert Byte[] to String using new String() String crunchifyDecodedData = new String ( crunchifyByte3 ) ; System . out . println ( "\nDecrypted String : " + crunchifyDecodedData ) ; // Method-5: Convert Byte[] to String using UTF_8 encoding String decodedDataUsingUTF8 ; // UTF_8 is Eight-bit UCS Transformation Format. decodedDataUsingUTF8 = new String ( crunchifyByte3 , StandardCharsets . UTF_8 ) ; System . out . println ( "Text Decrypted using UTF-8 : " + decodedDataUsingUTF8 ) ; } } |
คอนโซล Eclipse
เพียงเรียกใช้โปรแกรมด้านบนเป็น Java Application และคุณควรเห็นผลลัพธ์ที่คล้ายคลึงกัน

1 2 3 4 5 6 7 8 9 10 11 12 13 |
crunchifyString : Crunchify Example on Byte [ ] to String . . . crunchifyByte : [ 67 , 114 , 117 , 110 , 99 , 104 , 105 , 102 , 121 , 32 , 69 , 120 , 97 , 109 , 112 , 108 , 101 , 32 , 111 , 110 , 32 , 66 , 121 , 116 , 101 , 91 , 93 , 32 , 116 , 111 , 32 , 83 , 116 , 114 , 105 , 110 , 103 , 46 , 46 , 46 ] crunchifyString2 : This is test for converting String to byte . . . ! crunchifyByte2 : [ 84 , 104 , 105 , 115 , 32 , 105 , 115 , 32 , 116 , 101 , 115 , 116 , 32 , 102 , 111 , 114 , 32 , 99 , 111 , 110 , 118 , 101 , 114 , 116 , 105 , 110 , 103 , 32 , 83 , 116 , 114 , 105 , 110 , 103 , 32 , 116 , 111 , 32 , 98 , 121 , 116 , 101 , 46 , 46 , 46 , 33 ] crunchifyByte2 encoded value : VGhpcyBpcyB0ZXN0IGZvciBjb252ZXJ0aW5nIFN0cmluZyB0byBieXRlLi4uIQ == crunchifyByte2 decoded value : [ 84 , 104 , 105 , 115 , 32 , 105 , 115 , 32 , 116 , 101 , 115 , 116 , 32 , 102 , 111 , 114 , 32 , 99 , 111 , 110 , 118 , 101 , 114 , 116 , 105 , 110 , 103 , 32 , 83 , 116 , 114 , 105 , 110 , 103 , 32 , 116 , 111 , 32 , 98 , 121 , 116 , 101 , 46 , 46 , 46 , 33 ] Decrypted String : This is test for converting String to byte . . . ! Text Decrypted using UTF - 8 : This is test for converting String to byte . . . ! Process finished with exit code 0 |
แจ้งให้เราทราบหากคุณพบปัญหาใด ๆ ที่ทำงานอยู่เหนือโปรแกรม