Who knows Integer.byteValue()

Who knows why:int x = 123;byte b = Integer.byteValue(x);does not work, but:String tmpStr = Character.toString(tmpChar);orString tmpStr = Integer.toString(tmpInt);does work?Any help is much appreciated
[263 byte] By [apassow] at [2007-9-30 22:17:33]
# 1

form where did you bring the Integer.byteValue(int) method?!!

It's not exist in the Integer class, there is a method with byteValue() with no arguments, for any help about classes and methods of the J2SE refer to the online API documentations

http://java.sun.com/reference/api/index.html

or download it

http://java.sun.com/j2se/downloads/index.html

MohdSleem at 2007-7-7 11:30:48 > top of Java-index,Administration Tools,Sun Connection...
# 2
thank for the quick reply!!I guess I have not had a look and just asumed there would be one since Char, String, etc. have one.Do you know a good way of converting Int to byte or byte[] ? I mean does the API provide one?Again thanks and best wishes,Adrian
apassow at 2007-7-7 11:30:48 > top of Java-index,Administration Tools,Sun Connection...
# 3
Just cast it:int x = 100;byte b = (byte)i;Of course, if your int value is greater than the highest possible byte value (127) then this will have unexpected results.
scorbett at 2007-7-7 11:30:48 > top of Java-index,Administration Tools,Sun Connection...
# 4
Of course that should have been:int i = 100;byte b = (int) i;
scorbett at 2007-7-7 11:30:48 > top of Java-index,Administration Tools,Sun Connection...
# 5

use type casting

int num = 0;

byte b = (int) num;

Note: int is 4 bytes, where byte is only one byte, so casting int to byte maybe lossy is the int contains number that byte can't hold, refer to reference about conversion and casting for more help

Mohammed Saleem

MohdSleem at 2007-7-7 11:30:48 > top of Java-index,Administration Tools,Sun Connection...
# 6
Doh! Third time's a charm:int i = 100;byte b = (byte) i;
scorbett at 2007-7-7 11:30:48 > top of Java-index,Administration Tools,Sun Connection...
# 7
oppps!I meantbyte b = (byte) num;sorry for this mistake!
MohdSleem at 2007-7-7 11:30:48 > top of Java-index,Administration Tools,Sun Connection...
# 8
At least I'm not the only one who can't get it together on monday mornings.
scorbett at 2007-7-7 11:30:48 > top of Java-index,Administration Tools,Sun Connection...
# 9
Sure!it's Sunday 11:36 pm here
MohdSleem at 2007-7-7 11:30:48 > top of Java-index,Administration Tools,Sun Connection...
# 10
Sorry guys for all the trouble :) and thanks for the many results, this seems to be getting a nice discussion.So method which takes a int and breakes it into 127s and stores it into a byte[] ? - I gues that is what I will do.Thx
apassow at 2007-7-7 11:30:48 > top of Java-index,Administration Tools,Sun Connection...
# 11

You can use the ByteBuffer class or use shifting./* ByteBuffer */

public static byte[] toByteArray(int v) {

ByteBuffer bb = ByteBuffer.allocate(4);

bb.putInt(v);

return bb.array();

}

/* Shifting */

public static byte[] toByteArray(int v) {

byte[] result = new byte[4];

result[0] = (byte) (v >>> 24);

result[1] = (byte) (v >>> 16);

result[2] = (byte) (v >>> 8);

result[3] = (byte) v;

return result;

}

atmguy at 2007-7-7 11:30:48 > top of Java-index,Administration Tools,Sun Connection...