Results 1 to 3 of 3
  1. #1
    myka is offline Member
    Join Date
    Mar 2010
    Rep Power

    Default String byte array


    I have a string variable containg a 8bit binary data and I need to convert it to
    byte array.


    String ss ="11101000";
    I want this in a 8bit byte array.

    I tried

    Java Code:
    byte b = Byte.parseByte(ss,2);
    but getting this error--->

    Java Code:
    java.lang.NumberFormatException: Value out of range. Value:"11101000" Radix:2
    	at java.lang.Byte.parseByte(
    	at MsgDecode.main(

    I think this is because i did something wrong with the radi..i guess:confused:

    can someone please tell me to do this right? because i need to convert the string in to "1010" and need to shift the bits within a byte data block..
    Last edited by myka; 03-17-2010 at 01:47 PM.

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Zurich, Switzerland
    Rep Power


    Quote Originally Posted by myka View Post
    I think this is because i did something wrong with the radi..i guess:confused:

    your radix is ok. the problem is Byte.MIN_VALUE = -128 and Bye.MAX_VALUE = 127 and java don't let you convert String out of this range. there is a general solution to this problem. for the representation of your value a byte type is used, but for all other operations the value is converted to and int. here a small example:

    Java Code:
    public class UnsignedByteExample {
    	public static void main(String[] args) {
    		 Integer b2 = Integer.parseInt("11101000", 2);
    		 byte b = fromUnsignedInt(b2);
    	public static byte fromUnsignedInt(int a) {
    		return (byte) a;

    the code above runs without NumberFormatException and outputs 232, because the operation is done with an int and only at the end the result (representation) is converted to a byte. i got this solution from the book - Das Handbuch der Java-Programmierung for the source-code see and then open the file
    Last edited by j2me64; 03-17-2010 at 08:16 PM.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Colombo, Sri Lanka
    Blog Entries
    Rep Power


    It's not that much complicated.

    Java Code:
            String stringToConvert = "10100011";
            byte[] theByteArray = stringToConvert.getBytes();

Similar Threads

  1. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 11:32 AM
  2. String from byte array
    By justint in forum New To Java
    Replies: 2
    Last Post: 01-22-2010, 06:58 AM
  3. Search a string in a byte array
    By 2BOrNot2B in forum New To Java
    Replies: 0
    Last Post: 03-12-2009, 05:52 PM
  4. String array to byte array?!
    By Joe2003 in forum Advanced Java
    Replies: 5
    Last Post: 02-28-2009, 06:09 AM
  5. String byte storage
    By bozovilla in forum New To Java
    Replies: 1
    Last Post: 11-24-2007, 06:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts