Java Question: Taking Input String - Converting to byte[] - now I need to convert byte[] back to the input string?

Posted on 2009-04-18
Last Modified: 2012-05-06
I have an input str, that I convert to a byte[], and now i need to convert the byte[] back to the original input string.  How is that best accomplished?
Question by:mtitania
    LVL 86

    Expert Comment

    String s = new String(bytes)
    LVL 92

    Accepted Solution

    String has a constructor for that

    byte[] bytes = s.getBytes();
    String s = new String(bytes);

    or if a different encoding is used

    String s = new String(bytes, charset)
    LVL 86

    Expert Comment

    mtitania, can you tell me why you accepted an answer that was just a repeat of mine?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now