Reputation: 125
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();
Upvotes: 0
Views: 638
Reputation: 10239
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!
Upvotes: 2
Reputation: 3367
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.
Upvotes: 0
Reputation: 8868
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.
Upvotes: 0
Reputation: 117589
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);
Upvotes: 0
Reputation: 1616
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
Upvotes: 0
Reputation: 9741
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;
Upvotes: 2