简体   繁体   中英

Binary Representation to a new byte

I'm trying to create a new byte knowing a certain amount of bits

char prostie1 = theRepChars[j-3];
char prostie2 = theRepChars[j-2];
char prostie3 = theRepChars[j-1];
char prostie4 = theRepChars[j];
String prostiaMare = prostie4 + prostie3 + prostie2 + prostie1 + "";
Byte theChar = new Byte(prostiaMare);

When i do this I get a NumberFormatException value 196.

I have no idea what might be my problem

--EDIT--

Ok I think I might have to give some more details since I wasn't very clear. I'm trying to do an Uuencode algorithm and by following the logic of the algorithm I should stop my byte having a value bigger than 194. Here is a bunch of my code.

if(my_chars.length % 3 == 0)
    {

        for(int x = 0; x < my_chars.length; x++)
        {
                if((x+1) % 3 == 0)
                {
                    char first = my_chars[x-2];
                    char second = my_chars[x-1];
                    char third = my_chars[x];
                    int n = (((first << 8) | second) << 8) | third;
                    String theRep = Integer.toBinaryString(n);
                    while(theRep.length() < 24 - 1)
                    {
                        theRep = 0 + theRep;
                    }
                    //0 padded theRep
                    for(int j = 0; j < theRepChars.length; j++)
                    {
                        if((j+1) % 4 == 0)
                        {
                            char prostie1 = theRepChars[j-3];
                            char prostie2 = theRepChars[j-2];
                            char prostie3 = theRepChars[j-1];
                            char prostie4 = theRepChars[j];
                            String prostiaMare = prostie4 + prostie3 + prostie2 + prostie1 + "";
                            System.out.println(prostiaMare);

                        }
                    }



                }
        }


    }

And trying to create a new byte with the value that prostiaMare has gives me the numberFormatException. I'm not sure if I have not followed the algorithm right ( http://www.herongyang.com/encoding/UUEncode-Algorithm.html )

196 is outside the range of byte , a signed value. Bytes can range from -128 to 127.

I'm not sure why you're casting to String. If you just want a byte with bits equivalent those of the sum of the four chars, cast directly to byte:

(byte) (prostie4 + prostie3 + prostie2 + prostie1)

If you intended to construct a String from the four chars, you are not currently doing that. Use:

"" + prostie4 + prostie3 + prostie2 + prostie1

and, if the result is in the range of a byte, you can create a byte as you have been.

Bytes are signed in Java. Which means a byte , which is 8 bits long, has a minimum value of -2^7 (-128) and a max value of 2^7 - 1 (127). Java has no unsigned primitive types apart from char (unsigned, 16bit).

Therefore 196 is unparseable --> NumberFormatException .

You don't have much to work around this except to read into a larger type and do & 0xff to obtain the byte:

final int i = Integer.parseInt(theString);
final byte b = (byte) (i & 0xff);

Or do yourself a favour and use Guava, which has UnsignedBytes :

final byte b = UnsignedBytes.parseUnsignedByte(theString);

But it appears that you want to do comparisons anyway; so just use a larger type than byte . And no, this won't waste memory: don't forget about alignment.

As mentioned in the docs

An exception of type NumberFormatException is thrown if any of the following situations occurs:

  • The first argument is null or is a string of length zero .
  • The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX .
  • Any character of the string is not a digit of the specified radix, except that the first - character may be a minus sign '-' ('\-') provided that the string is longer than length 1.
  • The value represented by the string is not a value of type byte.

In your case its the last case since 196 cant be represented as byte..The valid range is -128 to 127

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM