0

I an having the string of array,i need to convert to bytes,So how can i do it?

sample is

String test[] = {"Hello","Hello1"};

I need to convert them to bytes in one place and get them back in other place as string. I need to pass in as bytes,I am having the string array,So is this way?or any other alternate mean?

5
  • string of array or array of strings? Commented Mar 8, 2012 at 9:47
  • 2
    Please explain in more detail, preferrably with some source code, what exactly you want to do and why - then we could give you much more useful answers. Commented Mar 8, 2012 at 9:47
  • Please paste the code that declares this "string of array", or is it "String array"? Did you mean: String[] array? Commented Mar 8, 2012 at 9:48
  • Do you mean you have String[] and you want a single byte[] containing a concatenation of all bytes from all String instances in the array? Commented Mar 8, 2012 at 9:48
  • you need to be more specific... what sort of bytes do you want? a serialised version of the string array? ASCII bytes? UTF-8? do you want delimiters for the array? also worth posting what code you have so far (SSCCE) - meta.stackexchange.com/questions/22754/…. Commented Mar 8, 2012 at 9:50

3 Answers 3

4

You can easily convert String[] to byte[][] by iterating the array and using

bytes[i] = str[i].getBytes(encoding);
Sign up to request clarification or add additional context in comments.

Comments

-1

Iterate through the String array, append each String value to a StringBuilder Object. After completing the iteration, return `builder.toString.getBytes()

public byte[] stringToByte(String[] str){

        StringBuilder builder = new StringBuilder();
        for(int i=0;i<str.length;i++){
            builder.append(str[i]);
        }
        return builder.toString().getBytes("UTF8");
    }

4 Comments

You need to provide an encoding to .getBytes(), or you will get a random encoding depending on the platform.
@ChristofferHammarström, yes, I agree. I just provided an example of "How to convert String[] to byte[]". It is up to the asker of this question to implement the desired encoding :-)
Then provide a correct example instead of a broken one. You know damn well people will copy and paste and don't understand why it suddenly breaks months later with strange characters on a different platform.
@ChristofferHammarström, I appreciate your effort. I have updated my answer.
-1
String Convertthis = "i want to convert this string to byte array" 
byte[] theByteArray = Convertthis.getBytes("UTF8");
System.out.println(theByteArray.length);

See http://javasourceblog.blogspot.in/2012/03/array-in-java.html

1 Comment

You need to provide an encoding to .getBytes(), or you will get a random encoding depending on the platform.

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.