Three methods in Java to convert byte to string

Source: Internet
Author: User

Address: http://www.cnblogs.com/521shina/archive/2009/03/19/1417204.html

The getresponsebody method of the getmethod class in the httpclient class library returns the byte [] type, which is inconvenient to operate. I want to convert it to the string type.

I checked the information on the Internet. I think it is better to use this method.
Base64encoder ENC = new base64encoder ();
String converted string = enc. encode (byte array );

Reference http://hi.baidu.com/zhaolm/blog/item/397b0808bc6023d362d986f3.html/cmtid/e3a206f43cb6f9e87609d746

But some people say this

Base64encoder is a non-official JDK class. Although it can be found and used in JDK, it cannot be found in API. These two may be used by Sun's insiders. No relevant documents can be found for the classes in the bag starting with sun, so they may all be unofficial classes. It is also reasonable and normal to receive a warning, because Sun may update or delete unofficial classes in the future. We recommend that you do not use it.

Having this disadvantage, it is quite troublesome to import the jar package, so I gave up using it.

So I checked three methods on an English website, which are relatively simple. I have used 3rd types, but it seems no problem at present.

From http://www.javadb.com/convert-byte-to-string

/** * * @author javadb.com */public class Main {        /**     * Example method for converting a byte to a String.     */    public void convertByteToString() {                byte b = 65;                //Using the static toString method of the Byte class        System.out.println(Byte.toString(b));        //Using simple concatenation with an empty String        System.out.println(b + "");                //Creating a byte array and passing it to the String constructor        System.out.println(new String(new byte[] {b}));            }        /**     * @param args the command line arguments     */    public static void main(String[] args) {        new Main().convertByteToString();    }}

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.