1

i need to convert from string to same byte array:

String data = request.getParameter("data");

byte[] dataByte = new byte[]{};

data = -60,-33,-10,-119,126,114,-61,-31,55,-102,-35,-72,114,77,115,72,79,-117,102,64,98,-20,-75,27,58,-59,86,-97,106,19,-112,-79,100,105,115,107,100,105,114,101,99,116,111,114,95,115,97,102,101,46,99,111,110,102,105,103,49,52,53,53,83,112,97,99,101,115,83,116,111,114,101,114,117,99,111,110,116,101,110,116,85,114,108,61,115,116,111,114,101,58,47,47,50,48,49,50,47,49,48,47,49,47,57,47,53,50,47,57,100,48,48,48,48,55,97,45,54,50,48,48,45,52,54,52,102,45,97,48,48,97,45,50,52,97,100,52,98,100,55,50,53,53,48,46,98,105,110,124,109,105,109,101,116,121,112,101,61,97,112,112,108,105,99,97,116,105,111,110,47,111,99,116,101,116,45,115,116,114,101,97,109,124,115,105,122,101,61,52,53,57,53,49,124,101,110,99,111,100,105,110,103,61,85,84,70,45,56,124,108,111,99,97,108,101,61,114,117,95,124,105,100,61,52,49,48,56,101,57,100,100,50,100,56,45,56,102,54,97,45,52,54,55,54,45,56,53,99,57,45,50,52,54,102,55,57,57,55,101,102,48,99,77,111,110,32,79,99,116,32,48,49,32,48,57,58,53,50,58,49,53,32,78,79,86,83,84,32,50,48,49,50,119,111,114,107,115,112,97,99,101,97,100,109,105,110,97,100,109,105,110
1
  • 1
    split(",") then Byte.parseByte() Commented Oct 9, 2012 at 10:40

5 Answers 5

2
String[] dataArray = data.split(",")

You can then iterate over that dataArray and then create byte[] out of it.

    String dataArray[] = data.split(",");
    byte[] bytes = new byte[dataArray.length];
    int count = 0;
    for(String str : dataArray)
    {
        bytes[count++] = Byte.parseByte(str);
    }

If you know character encoding then you can use #String.getBytes

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

2 Comments

String.getBytes() semantics is quite different from what I suspect the OP wants and you indicate with your code fragment - that will give you the byte representation of the string, not the byte values that are comma-separated!
Problem is it is not clear how he has obtained that data string. If he wants to convert exact sequence then solution 1 will work. But it can be a simple print of byte[] which was created from string. That is why I have added second point.
0

String.getBytes(String charsetName) will give you a byte-array. You will have to be careful re. specifying your character encoding.

Comments

0

Use StringTokenizer or String.split() method and parse each substring to byte. Take care with encoding.

Comments

0

Use String.split() and iterate over the resulting array:

String[] array = data.split(",");
byte[] dataByte = new byte[ array.length ];
for ( int i=0 ; i<array.length ; i++ ) { dataByte[ i ] = Byte.parseByte( array[ i ] ); }

Cheers,

1 Comment

It's not clear, but I'm not convinced that the OP is getting a comma-separated string. I think that's more likely an illustration of the info they want
0

You can use String#getBytes() method for this purpose: -

String data = request.getParameter("data");
byte[] dataByte = data.getBytes()

It uses the default character encoding.. You can specify your own..

See String#getBytes(Charset)

Or you can use Byte#parseByte(String) method in each value of the string array after splitting it: -

    String data = request.getParameter("data");
    String arr[] = data.split(",");
    byte[] newarr = new byte[arr.length];

    int count = 0;
    for (String val: arr) {
        newarr[count++] = Byte.parseByte(val);
        System.out.println(Byte.parseByte(val) + ", ");
    }

1 Comment

I don't think that is what the OP wants - this would give you the byte representation of the string, not the byte representation of the values separated by commas in the string...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.