0

I have written the following program in Java to convert long to byte.

    public class LongtoByte 
    {
            public static void main(String[] args)
            {
                long a=222;
                byte b=(byte)(a & 0xff);
                System.out.println("the value of b is" +b);

            }
    }

The problem is I get the result -34 for the variable b.

Please tell me how to get the correct value. I want the value in bytes only.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
user1673627
  • 271
  • 3
  • 6
  • 13

5 Answers5

2

Java's types are signed, bytes allow numbers between −128 and +127.this is the reason you were getting −34 for 222 value

     long a=121;
     byte b=(byte)(a );
     System.out.println("the value of b is" +b);
Joey
  • 344,408
  • 85
  • 689
  • 683
thar45
  • 3,518
  • 4
  • 31
  • 48
  • You have taken long a=121 but I want long a=222 to be converted into byte value so that it can be stored in a byte array – user1673627 Sep 21 '12 at 06:22
1

All integer types (including byte) are signed in Java, so if you stick 222 into a Java byte you get an overflow (resulting in the negative number you saw). If you need the range 0–255 for a integral number in Java you'll need at least a short.

However, if you're just going to write that result somewhere as a single byte you don't need to worry, as its bit pattern representation is exactly the same as 222 in an unsigned byte.

Joey
  • 344,408
  • 85
  • 689
  • 683
0

You can use the java.lang.Long class' byteValue() method:

byte b = a.byteValue();

You will have to make a Long type object as such:

Long a = new Long(222);

And as others have noted, this will return -34 due to overflow of the range that can be represented by a byte which is 8 bits.

squiguy
  • 32,370
  • 6
  • 56
  • 63
0

When you print a byte it assumes a range of -128 to 127.

If you print

byte b = (byte) 222;

you should expect to get a negative number.

If you want to store the range 0 to 255 you need to convert it when you get the value out.

int i = 222;
byte[] b = { (byte) i };
int i2 = b[0] & 0xFF; // give me the original unsigned 0 to 255.
assert i == i2;

You can invent all sorts of encoding. e.g. Say you want to store numbers which are only in millions say 0 to 200 million or decimal numbers -1.00 to 1.00 in a byte. You might first think this is impossible because a byte only stores 8 bits.

// store millions.
byte b = (byte) (i / 1000000);
int i = (b & 0xff) * 1000000;

// store decimal from -1.00 to 1.00
byte b = (byte) Math.round(d * 100);
double d = b / 100.0;
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • But peter it is ok for 222. But what if I want to convert any number such as 12000 or 120000. It does not work at that time. How to make it work? – user1673627 Sep 21 '12 at 08:48
  • You can only store 256 different values in a single byte. If you know all possible values in advance and you can store any number you like using a lookup table or a formula (as in the example) – Peter Lawrey Sep 21 '12 at 08:51
  • Peter you are still storing -34 in the byte array b and in the next line you are ANDing b[0] with 0xFF and storing in integer variable. But I want to store the actual long value after converting into byte in the byte array and I do not want to store -34. Let me tell you I want this byte value because I want to transfer this byte array over a TCP connection and using byte array will be fast. – user1673627 Sep 21 '12 at 08:56
  • A byte is 8-bits, You are not storing -34 or 222, You are storing `11011110` in binary. What those bits mean is purely an illusion. It doesn't matter where the data is going or how it is transmitted you will have the same bits at the end as the start. All that matters is the receiver is able to decode the values you sent. – Peter Lawrey Sep 21 '12 at 08:58
  • So, peter, according to you will it be fast to use integer instead of byte to store any value to send over a tcp connection? – user1673627 Sep 21 '12 at 09:02
  • I don't see how this is related to the question or my answer but can you qualify "fast" for me? e.g. what delay in nano-seconds is acceptable? What network do you have? – Peter Lawrey Sep 21 '12 at 09:06
  • Can you give me some numbers? You cannot say is `delay-seen < delay-acceptable` unless you have some idea of the numbers involved. – Peter Lawrey Sep 21 '12 at 09:15
-1
public class LongtoByte 
    {
            public static void main(String[] args)
            {
                long a=222;
                byte b=(byte)(a);
                System.out.println("the value of b is" +b);

            }
    }

This byte bValue = (byte) num; statement is converted into a byte format.

Sundar G
  • 1,069
  • 1
  • 11
  • 29