3

Actually I need to transfer the integer value along with the bitmap via bluetooth.. Now my problem is I need to transfer the integer as single byte value.. Is tat possible to convert int as single byte value.. and retrieve it as a integer there... I tried byteValue() and the casting thing but its not usefull.. If my approach is right just help me out with this or say some other way.

(Each time when I am using casting then it's returning as 65535)

Cœur
  • 37,241
  • 25
  • 195
  • 267
Hussain
  • 5,552
  • 4
  • 40
  • 50
  • I also tried to do with converting `int` to `char` and trying to convert as `byte` (eventhough it work for only till 255)but its not of use... – Hussain Apr 01 '11 at 07:27
  • try to use serialization if you can not able to send int value directly – Vivek Apr 01 '11 at 07:32
  • Why not directly use byte value and not use integer variable at all? – Gautham Aug 12 '12 at 16:25

4 Answers4

13

What about this?

public static byte[] intToByteArray(int a)
{
    byte[] ret = new byte[4];
    ret[3] = (byte) (a & 0xFF);   
    ret[2] = (byte) ((a >> 8) & 0xFF);   
    ret[1] = (byte) ((a >> 16) & 0xFF);   
    ret[0] = (byte) ((a >> 24) & 0xFF);
    return ret;
}

and

public static int byteArrayToInt(byte[] b)
{
    return (b[3] & 0xFF) + ((b[2] & 0xFF) << 8) + ((b[1] & 0xFF) << 16) + ((b[0] & 0xFF) << 24);
}
Dante May Code
  • 11,177
  • 9
  • 49
  • 81
  • @Dante - he asked about a *single* byte. – MByD Apr 01 '11 at 07:53
  • @MByD, a single byte can not convey all the info, so I provided array to be transferred byte by byte. – Dante May Code Apr 01 '11 at 08:01
  • @Dante: I need it in single byte.. Then i cant able to diifferentiate the `Bitmap` and the `int` – Hussain Apr 01 '11 at 08:25
  • @Dante: Other answers in the sense? That casting is not at all working.. To be clear im sending the length of the bitmap along with some data.. So i need tat length in one byte.. Is tat possible.. – Hussain Apr 01 '11 at 08:38
  • @Dante: This the **format** im going to send via `Bluetooth` **( CHAR | CHAR | CHAR | LENGTH | BITMAP )** Now from this i have to take the length to get back the `Bitmap` in the other mobile.. Now is it clear? – Hussain Apr 01 '11 at 09:02
  • @Hussain, I hope you are able to change the format. Is it possible? By the way, `char` in Java is 2 bytes. – Dante May Code Apr 01 '11 at 09:09
  • @Dante: Yeah possible but anyway i need the `int` ie(**LENGTH**).. How to do tat..? is there any way? – Hussain Apr 01 '11 at 09:11
  • @Hussain, since you are able to get `CHAR|CHAR|CHAR`, I think you can get `CHAR|CHAR|CHAR|INT`. No conversion is needed.... If my assumption is wrong (and you cannot read `INT`), then I am sure you can get `CHAR|CHAR|CHAR|CHAR|CHAR`, the last 2 `CHAR|CHAR` is the `CHAR` array of `INT`. It is similar as the conversion of `BYTE` array I showed in my answer. – Dante May Code Apr 01 '11 at 09:18
  • @Dante: Thanks Dante i will work with tat.. Thanks for ur great help.. :) – Hussain Apr 01 '11 at 09:27
  • @Hussain, if it truly works, don't forget to check my answer. XD – Dante May Code Apr 01 '11 at 09:48
  • we can revert int like this 'ByteBuffer.wrap(byte[] ).getInt()' – Mateen Chaudhry Nov 01 '20 at 10:24
10

If you're completely sure, that your int variable contains a byte value [-128; 127] then it should be as simple as:

int i = 100; // your int variable
byte b = (byte) i;
Konstantin Burov
  • 68,980
  • 16
  • 115
  • 93
  • Its not working while converting back to `int`.. Its returning some **default** value for every `int` – Hussain Apr 01 '11 at 08:35
  • @Hussain how and when do you convert it back? – Konstantin Burov Apr 01 '11 at 08:47
  • I will be passing it as a `ByteArray` thro `Bluetooth` and separate there as a `int` and other data.. With the help of **length** i will be getting the `Bitmap` available in tat.. – Hussain Apr 01 '11 at 08:53
  • @Hussain you'd have to post some code, your comment doesn't makes it any clearer. – Konstantin Burov Apr 01 '11 at 08:57
  • The possible code i have tried are not working.. So only i sent a comment alone... This the **format** im going to send via `Bluetooth` **( CHAR | CHAR | CHAR | LENGTH | BITMAP )** Now from this i have to take the length to get back the `Bitmap` in the other mobile.. – Hussain Apr 01 '11 at 09:04
2

A single byte (8 bits) can only contain 2^8 unsigned integers, i.e [0, 255]. For signed you loose the first bit and the range becomes [-128, 127]. If your integer fits then a simple cast should work.

sthysel
  • 377
  • 3
  • 8
1

for 0-255 numbers.

int i = 200; // your int variable
byte b = (byte)(i & 0xFF);
hamish
  • 1,141
  • 1
  • 12
  • 21