Today, I am going to hash out 1 of the mutual tasks for programmers, converting a String to a byte array. You demand to make that for multiple reasons e.g. for saving content to a file, sending over a network or perhaps another reason. Suppose you lot receive got a String "abcd" as well as you lot desire to convert it into a byte array, how volition you lot make that inwards a Java program? Remember, String is made of the char array, thus it involves grapheme to byte conversion, which is dependent area to character encoding intricacies. Thankfully, Java provides a convenient getBytes() method to convert String to byte array inwards Java, simply unfortunately, many developers don't purpose it correctly. Almost 70% of the code I receive got reviewed uses getBytes() without grapheme encoding, leaving it on the lead chances that platform's default grapheme encoding volition endure same every bit of the source String.
The right way to purpose getBytes() should e'er endure amongst explicit grapheme encoding, every bit shown inwards this article. Java fifty-fifty comes amongst unopen to touchstone fix of grapheme encoding which is supported out-of-box yesteryear StandardCharset class, nosotros volition review them every bit well.
It's likewise a proficient exercise is to purpose the pre-defined contestants for specifying grapheme encoding inwards your code instead of using a gratis text or String to avoid typos as well as other airheaded mistakes.
In past, I receive got shown you lot how to convert a byte array to String inwards Java as well as inwards this article, I volition present you lot iii mutual ways to convert a String to byte array inwards Java, let's get-go amongst the most pop one.
This is the most mutual way to convert a String into a byte array, it plant most of the fourth dimension simply it's error-prone as well as tin make an erroneous number if platform's grapheme encoding doesn't jibe amongst expected encoding.
Here is an illustration of converting String to byte[] inwards Java :
Remark :
1) Platform's default encoding is used for converting a grapheme to bytes if you lot don't specify whatever grapheme encoding.
2) You tin come across platform's default grapheme encoding yesteryear using System.getProperty("file.encoding");, this render the default grapheme encoding of the machine your JVM is running. You tin see checked exception java.io.UnsupportedEncodingException, if grapheme encoding String has a typo or specifies as well as grapheme encoding non supported yesteryear Java.
2) The returned byte array is on specified grapheme encoding
3) You tin come across that length of the byte array is not same every bit a number of characters inwards String every bit was the illustration inwards the previous illustration because UTF-16 encoding takes at-least 2 bytes to encode a character.
H5N1 proficient affair nigh this approach is that it doesn't throw checked java.io.UnsupportedEncodingException, simply unfortunately this shape is alone available from JDK seven onward thus it mightiness non endure an option for several Java application running on Java six as well as lower version.
Remarks :
1) This is the best way to convert String to a byte array inwards Java.
2) This doesn't throw java.io.UnsupportedEncodingException exception, which agency no boilerplate code for treatment this checked exception.
3) Though, you lot must continue inwards in hear that StandarhardCasets shape is alone available from Java seven onward. You tin come across Core Java for the Impatient for to a greater extent than such details, which likewise covers Java SE 8.
That's all nigh how to convert a String to byte array inwards Java. Remember the size of byte array tin endure to a greater extent than than the length of String because it's non necessary that 1 byte is used to encode 1 character, it all depends on grapheme encoding. For example, UTF-8 is a multi-byte grapheme encoding system as well as uses betwixt 1 to four bytes per character. In general, characters of the former ASCII arrive at takes 1 bytes simply characters from the former ISO-8859 arrive at beyond ASCII takes 2 bytes.
Further Learning
Data Structures as well as Algorithms: Deep Dive Using Java
How to supervene upon characters as well as substring inwards given String?
How String inwards switch illustration plant inwards Java?
What is the deviation inwards String puddle betwixt Java six as well as 7?
The right way to purpose getBytes() should e'er endure amongst explicit grapheme encoding, every bit shown inwards this article. Java fifty-fifty comes amongst unopen to touchstone fix of grapheme encoding which is supported out-of-box yesteryear StandardCharset class, nosotros volition review them every bit well.
It's likewise a proficient exercise is to purpose the pre-defined contestants for specifying grapheme encoding inwards your code instead of using a gratis text or String to avoid typos as well as other airheaded mistakes.
In past, I receive got shown you lot how to convert a byte array to String inwards Java as well as inwards this article, I volition present you lot iii mutual ways to convert a String to byte array inwards Java, let's get-go amongst the most pop one.
String to byte array using getBytes()
This is the most mutual way to convert a String into a byte array, it plant most of the fourth dimension simply it's error-prone as well as tin make an erroneous number if platform's grapheme encoding doesn't jibe amongst expected encoding.Here is an illustration of converting String to byte[] inwards Java :
// converts String to bytes using platform's default grapheme encoding, // inwards Eclipse it's Cp1252 // inwards Linux it could endure something else byte[] ascii = "abcdefgh".getBytes(); System.out.println("platform's default grapheme encoding : " + System.getProperty("file.encoding")); System.out.println("length of byte array inwards default encoding : " + ascii.length); System.out.println("contents of byte array inwards default encoding: " + Arrays.toString(ascii)); Output : platform's default grapheme encoding : Cp1252 length of byte array inwards default encoding : 8 contents of byte array inwards default encoding: [97, 98, 99, 100, 101, 102, 103, 104]
Remark :
1) Platform's default encoding is used for converting a grapheme to bytes if you lot don't specify whatever grapheme encoding.
2) You tin come across platform's default grapheme encoding yesteryear using System.getProperty("file.encoding");, this render the default grapheme encoding of the machine your JVM is running. You tin see checked exception java.io.UnsupportedEncodingException, if grapheme encoding String has a typo or specifies as well as grapheme encoding non supported yesteryear Java.
2) The returned byte array is on specified grapheme encoding
3) You tin come across that length of the byte array is not same every bit a number of characters inwards String every bit was the illustration inwards the previous illustration because UTF-16 encoding takes at-least 2 bytes to encode a character.
String to byte array using getBytes(Charset)
This is 3rd simply in all probability the best way to convert to String to byte[] inwards Java. In this example, I receive got used java.nio.StandardCharsets to specify grapheme encoding. This shape contains unopen to of the widely used grapheme encoding constants e.g. UTF-8, UTF-16 etc.H5N1 proficient affair nigh this approach is that it doesn't throw checked java.io.UnsupportedEncodingException, simply unfortunately this shape is alone available from JDK seven onward thus it mightiness non endure an option for several Java application running on Java six as well as lower version.
// render bytes inwards UTF-8 grapheme encoding // pros - no demand to handgrip UnsupportedEncodingException // pros - bytes inwards specified encoding scheme byte[] utf8 = "abcdefgh".getBytes(StandardCharsets.UTF_8); System.out.println("length of byte array inwards UTF-8 : " + utf8.length); System.out.println("contents of byte array inwards UTF-8: " + Arrays.toString(utf8)); Output : length of byte array in UTF-8 : 8 contents of byte array in UTF-8: [97, 98, 99, 100, 101, 102, 103, 104]
Remarks :
1) This is the best way to convert String to a byte array inwards Java.
2) This doesn't throw java.io.UnsupportedEncodingException exception, which agency no boilerplate code for treatment this checked exception.
3) Though, you lot must continue inwards in hear that StandarhardCasets shape is alone available from Java seven onward. You tin come across Core Java for the Impatient for to a greater extent than such details, which likewise covers Java SE 8.
That's all nigh how to convert a String to byte array inwards Java. Remember the size of byte array tin endure to a greater extent than than the length of String because it's non necessary that 1 byte is used to encode 1 character, it all depends on grapheme encoding. For example, UTF-8 is a multi-byte grapheme encoding system as well as uses betwixt 1 to four bytes per character. In general, characters of the former ASCII arrive at takes 1 bytes simply characters from the former ISO-8859 arrive at beyond ASCII takes 2 bytes.
Further Learning
Data Structures as well as Algorithms: Deep Dive Using Java
How to supervene upon characters as well as substring inwards given String?
How String inwards switch illustration plant inwards Java?
What is the deviation inwards String puddle betwixt Java six as well as 7?
When to purpose intern() method of String inwards Java?
Difference betwixt String literal as well as novel String inwards Java?
Difference betwixt String literal as well as novel String inwards Java?
10 Difference betwixt StringBuffer as well as StringBuilder inwards Java?
Thanks for reading this article, if you lot similar this tutorial as well as thus delight part amongst your friends. If you lot receive got whatever questions or feedback as well as thus delight drib us a note.
Thanks for reading this article, if you lot similar this tutorial as well as thus delight part amongst your friends. If you lot receive got whatever questions or feedback as well as thus delight drib us a note.
No comments:
Post a Comment