2

the following code is the code to convert string into byte[]

byte[] feedback;
feedback = "Your answer is correct and submit on time".getBytes();

but i found that the byte[] is containing a series of numbers, how do i convert back to string "Your answer is correct and submit on time"?

thanks

2
  • 1
    The numbers you see is byte/ASCII code. Commented May 7, 2012 at 9:52
  • 1
    Your requirement is built-in in String class!!! String has several constructor and methods for this goals. You could find your answer by reading String JavaDocs. Commented May 7, 2012 at 9:54

6 Answers 6

5
String s = new String(feedback)

But note that both getBytes() and new String() have versions that take an encoding, and you really, really, REALLY should use that, after reading The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

Sign up to request clarification or add additional context in comments.

Comments

1

You should be able to use this constructor to get your string back: String newString = new String(feedback);

Comments

1
String s = new String(feedback);

You should try first to search on the docs... http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

Comments

0
byte[] feedback = "Your answer is correct and submit on time".getBytes ();

String backtoString = new String (feedback);

Comments

0

You can convert your byte array back to a string using the String constructor:

String yourString = new String(feedback);

Bear in mind that both conversions are based on the default character endoding (UTF8, ...). Other methods allow setting a specific encoding; also, there is a toCharArray() method giving you an encoding-independent char[] instead of byte[].

Comments

0

Try this

String u = new String(feedback, "UTF8");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.