-1

there is a string fill with 0 and 1,like String s = "10000000", which length is 8.And how can i transform it to a byte.such as "10000000"===>-128. I try to use Byte.parseByte(s, 2),but get the error "Value out of range. Value:"10000000" Radix:2".So,how can i solve it.

2

1 Answer 1

1

You need to parse it as an Integer and then cast it to byte:

...
String s = "10000000";
int val = Integer.parseInt(s, 2);
byte b = (byte) val;
System.err.println(b);
...

Output:

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

1 Comment

thank you very much

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.