3 Ways to convert a string to a byte array in Java __java

Source: Internet
Author: User

Today, I'll discuss one of the common tasks of programmers, converting a string into a byte array. There may be several reasons for doing this (saving content to a file, sending it over a network, or other reason). Suppose you have a string "ABCD" and you want to convert it into a byte array, what would you do. Remember that a string is composed of a char array, so it involves converting characters to bytes. Thankfully, Java provides a convenient GetBytes () method to convert a string into a byte array in Java, but unfortunately many developers do not use the correct method. Approximately 70% using the GetBytes () method does not specify a character encoding, but instead uses the platform default character encoding. Most of this can work, but error prone, if the platform's character encoding does not match the expected encoding, it will produce the wrong result.

String to byte array using getBytes ()

Converts String to bytes using platform ' s default character encoding, 
//In Eclipse it's Cp1252 
//in Linux it Could be something else 

byte[] ascii = "ABCDEFGH". GetBytes (); 
System.out.println ("platform ' s default character encoding:" + system.getproperty ("file.encoding")); 
System.out.println ("Length of byte array in default encoding:" + ascii.length); 
System.out.println ("Contents of byte array in default encoding:" + arrays.tostring (ASCII)); 

Output: 
platform ' s default character encoding:cp1252 
length of byte array in default Encoding:8 contents of B Yte array in default encoding: [97, 98, 99, 100, 101, 102, 103, 104]

Note:
1 if no character encoding is specified, the character is converted to bytes using the platform default encoding.

2 You can use System.getproperty ("file.encoding") to view the platform's default character encoding, which returns the default character encoding for the machine that the JVM is running.

3 Be careful, your code may run in one environment without problems, but it cannot be guaranteed to run in other environments as well. This is why you should not rely on the default character encoding.

4 The length of the byte array may be inconsistent with the length of string, depending on the character encoding.

String to byte array using getBytes ("encoding")

Convert String to bytes the specified character encoding but
//also throw checked unsupportedencodingexception, whi CH pollutes The code 
try { 
    byte[] utf16 = "Abcdefgh". GetBytes ("UTF-16"); 
    System.out.println ("Contents of byte array in UTF-16 encoding:" + arrays.tostring (UTF16)); 
catch (Unsupportedencodingexception e) { 
    e.printstacktrace (); 
} 

Output: 
length of byte array in UTF-16 charater encoding:18 
contents to byte array in UTF-16 encoding: [-2,-1 , 0, 97, 0, 98, 0, 99, 0, 100, 0, 101, 0, 102, 0, 103, 0, 104]

Note:
1 This method is better than the above example, but throws a checked exception java.io.UnsupportedEncodingException, prevents the character-encoded string from being wrong, or specifies a character encoding that does not support Java.

2) returns a byte array of the specified character encoding

3 You can see that the length of the byte array differs from the number of characters in string, as in the previous example, because the UTF-16 encoding uses at least 2 bytes to encode characters.

String to byte array using GetBytes (Charset)

Return bytes in UTF-8 character encoding 
//pros-no need to handle unsupportedencodingexception 
//Pros-byt ES in specified encoding scheme 

byte[] UTF8 = "abcdefgh". GetBytes (standardcharsets.utf_8); 
System.out.println ("Length of byte array in UTF-8:" + utf8.length); 
System.out.println ("Contents of byte array in 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]

Note:
1 This is the best way to convert a string into a byte array in Java.

2) This does not cause java.io.UnsupportedEncodingException exceptions

3) Keep in mind that the Standarhardcasets class can only be provided from Java 7.

Original: http://www.java67.com/2017/10/3-ways-to-convert-string-to-byte-array-in-java.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.