5

I have a string that looks like this: ["1011000", "1000010", "1001101", "1000011"].

My argument is coming from elsewhere so it needs to be this way.

I need to typecast this to a real byte array.

Here's my method:

public void send(String[] payloadarr)  throws IOException { 
    byte [] payload = {};

    for (int i = 0; i < payloadarr.length; i++) {
        byte x = (byte) payloadarr[i];
        payload[i] = x;
    }
    //do byte stuff with payload
}

It doesn't work, however. Complains about inconvertable types String to byte.

Can anyone help me with this typecasting?

4
  • Did you try, String.getBytes() or String.getBytes(ENCODING)? I made this a comment because I'm not aware of your requirements. Commented Jul 13, 2011 at 1:10
  • 1
    Do you actually mean that you have an array of strings? Commented Jul 13, 2011 at 1:13
  • That is probably the wrong answer. Those strings look very much as if they are intended to be base-2 numeric representations of bytes. Yes - payloadarr is an ARRAY of strings. Commented Jul 13, 2011 at 1:20
  • 1
    BTW: This is not type casting but type conversion, or better, data parsing. Commented Jul 13, 2011 at 1:52

3 Answers 3

6

EDITED: Thanks to @Gabe for idea to change from Integer.parseInt to Byte.parseByte

You can use Byte.parseByte(String s, int radix) - your radix being 2 (ie base 2)

Here's a handy method to convert an array of String to byte[]:

public static byte[] stringsToBytes(String[] payloadarr) {
    byte[] payload = new byte[payloadarr.length];
    for (int i = 0; i < payloadarr.length; i++) {
        payload[i] = Byte.parseByte(payloadarr[i], 2);
    }
    return payload;
}

public static void main(String[] args) {
    System.out.println(Arrays.toString(stringsToBytes(new String[] { "1011000", "1000010", "1001101", "1000011" })));
}

Output:

[88, 66, 77, 67]
Sign up to request clarification or add additional context in comments.

1 Comment

@brian: If your problem is solved, accept the best answer (there is an "accept" button for you beside the answer).
3

It's hard to tell from your question, but it sounds like you really want

byte x = Byte.parseByte(payloadarr[i], 2);

2 Comments

This is the best answer. Looks like the other folks proffering answers didn't realize Byte.parseByte() existed. This avoids a cast, and while parsing as an int and then doing the narrowing cast does do the right thing with the sign bit, it might be a little surprising to newcomers when they find that e.g. Integer.parseInt("11011011")==219 but (byte)Integer.parseInt("11011011")==-37.
@prodicus: Not to mention which a (byte)Integer.parseInt("100000000") returns 0 instead of throwing an exception!
2

You can't do this using (just) type casts.

You need to use Integer.parseInt(String, int) where the int is 2. That will give you an int which you need to cast to a byte.

public void send(String[] payloadarr)  throws IOException { 
    byte [] payload = new byte[payloadarr.length];

    for (int i = 0; i < payloadarr.length; i++) {
        payload[i] = (byte) Integer.parseInt(payloadarr[i], 2);
    }
    //do byte stuff with payload
}

Notes

  • The above method will throw NumberFormatException if any of the components of payloadarr is not a binary string.

  • I fixed the initialization of payload ...

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.