0

I have the following code:

byte[] someArray;
int a,b;
.
.
.
a=123; (result coming from calculations, always 0>=a<256)
b=91; (result coming from calculations, always 0>=b<256)

now i want to do this

someArray[0]=a;
someArray[6]=b;

however i tried to convert in to byte to this i failed (getting all the possible error messages i think). also tried various snippets referring to integer to byte conversion.

so any idea?

update this is what i get java.lang.NumberFormatException: Invalid int: "z" when i try this byteArray[0]=Integer.valueOf(String.valueOf(bani.substring(2, 3)), 16).byteValue();

2
  • Have you tried someArray[0] = (byte) a? Commented Aug 28, 2013 at 10:59
  • i updated my answer in order to invoke an exception Commented Aug 28, 2013 at 14:10

6 Answers 6

2

integer -> byte conversion will result in a compilation error because it might result in precision loss. Documentation.

However, you can do an explicit cast:

someArray[0]=(byte)a;
Sign up to request clarification or add additional context in comments.

3 Comments

"Cannot cast from Integer to byte"... Anyway, i play in a range from 0 to 255, i can't lose anything.
Compiler keeps you from making mistakes. So it forces you to cast it explicitly.
if i do so, the application compiles, but it crashes when it gets to cast.
2

Be aware that Byte is not 0 to 255! It is -128 to +127

if you are aware of this, simply cast the values

someArray[0] = (byte) a;
someArray[6] = (byte) b;

But srround it with a check because java will cast you everything:

if(value <= Byte.MAX_VALUE && value >= Byte.MIN_VALUE) {
    //do the cast
}
else {
    //error handling
}

for example:

int i = 1300;
byte b = (byte) i;
//b will be 20 here

UPDATE:

If you want to envoke a NumberFormatException you can parse the value like this:

Byte.valueOf(Integer.toString(value));

Remember that byte is from -128 to 127!

Comments

0

In Java integral primitive types (byte, short, int, long) are always signed.

Hence a byte is always -128...127

You can convert an int to a byte by using casting

int i = 120;

byte b = (byte) i;

if you assign 200 to i instead of 120, then b becomes -56, assigning b back to i then i will remain -56 and not turn to 200 again.

This is because the binary representation of 200 is 11001000, and put these bits into a byte then it is interpreted as -56, putting this into an int again makes it into 1111111111111111111111001000

Comments

0

First of all, there is no unsigned data type in Java. All of them are signed byte is signed data type and can hold values in the range -128 <= b => 127. Secondly, you need to subtract 128 from the integer value and then cast it to byte, just to get a value that byte can hold:

someArray[0] = (byte) (a - 128);
someArray[6] = (byte) (b - 128);

3 Comments

-1 By doing this, if a = 1, someArray[0] has the value -127, see pastebin.com/sMDMUycm
It's also not true that there's no unsigned data type in Java. char is unsigned.
@dnet You didn't understand what I am saying. byte cannot hold the values > 127 since it is signed. What I suggested is to subtract 128 from the integer value just for getting a value that byte can hold.
0

I am not sure about how far the application you develop this for concerns memory usage :-). We have an API Bytes.toBytes() in HBase API. You can use this API to get a byte representation of a lot of types. May be you can use the reverse of it like Bytes.toInt() for converting back. As far i seen, no issue of precision loss or value change is seen.

You can download the lib from here Hbase Lib .. Add the hbase-0.94.jar from the lib to your project.

Comments

0

To guarantee no error for values above Byte.MAX_VALUE and below Byte.MIN_VALUE you could use bitwize operations to impose a max value:

public static byte toByte(int i){
    return (byte) (i & 0x000000ff);
}

This will ignore all bites above the first 8. Thus giving you a valid byte from any int (with the loss of the 24 highest order bits).

If you use this operation a lot this byte far faster than comparison and cast.

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.