Converting int to byte in java [duplicate] - java

This question already has answers here:
Assigning int to byte in java?
(5 answers)
Closed 8 years ago.
I have an int variable in java whose value is less than 256. So Can I store it in a Single byte variable.
I performed the following code.
int i =247;
byte b = (byte) i ;
But When I print it,
System.out.println(" i = "+(int)b);
It outputs
i = -9
Is it possible to convert int values less than 256 to single byte variables.
Pls. Help..

This isn't going to work. The MAX_VALUE for byte is 127 because bytes are signed in Java. (Consequently, the MIN_VALUE is -128. This is your typical Two's Compliment pattern.)
That being said, you have a couple options:
Leave it as an int and just use an int for your calculations. This might even be faster for you.
Leave it as a byte even if the number isn't correct. The numbers might not be correct in Java, but if all you care about is the bits being correct, they will be correct. So you can send that bite (which is a -9 in Java) across a webservice to another app that reads it as an unsigned byte, and it will read it as 247 correctly.

Related

Why does 0b1111_1111 cause a compiler error "int cannot be converted to byte" but not 0b111_1111? [duplicate]

This question already has answers here:
Understanding Java data types
(2 answers)
How to represent 11111111 as a byte in java
(2 answers)
JAVA: why binary literal for byte with negative sign is being considered as integer type?
(5 answers)
Closed 1 year ago.
byte b = 0b1111_1111;
byte c = 0b111_1111;
byte d = 0b0111_1111;
The first line causes a compilation error: incompatible types. I also tried 0xFF and decimal, the same. Thanks.
And I think byte means cannot be larger than what 8 bits hold. 0b1111_1111 is 8 bits.
All basic integer types in Java are signed. To be exact, byte, short, int and long are signed big endian two complement integers. That means that they run from - 2n-1 to 2n-1 - 1 where n is the number of bits - 8 for a byte (Byte.SIZE in Java), and therefore the range is from -128 to 127.
The number you are using is always an integer; number literals always are. However, the integer is actually the value 0x000000FF in hexadecimals. That cannot be stored as it is higher than 127. You can however do an explicit conversion; your value does consist of 8 bits after all.
byte b = (byte) 0b1111_1111
works because it simply disregards the 24 bits from the left hand side of the value. Now if you print it you will get -1 as value though as 0b1111_1111 represents 1 in 8 bit two complement form.
If you need it to represent the integer value 255 then you need to perform the following little trick:
int i = b & 0xFF;
which will actually do the following:
First it will convert b to the value 0xFFFFFFFF as integer using sign extension as all calculations default to integer, and it still thinks that b represents -1 after all.
Then it performs a bitwise AND with the value 0x000000FF (which is called a "mask"), resulting of course in the same value 0x000000FF which represents 255.
If you think this is a nuisance then you are right. Java should have used unsigned bytes, but it decided to use only one particular integer type that is signed. Maybe a bit overzealous but still a big improvement over e.g. C where you have way too many integer formats, and each of them may be represented differently on various machines.

Signed bytes in Java, What's the big idea? [duplicate]

This question already has answers here:
Why doesn't Java support unsigned ints?
(17 answers)
Closed 6 years ago.
Byte value is between 0 and 255, very simple and straight forward. Java didn't think so however and decided that values to be between -128 and 128 and ruined my life and many others.
I just want to know what's the big idea? why everytime i need to get the unsigned byte value do I have to do this:
int byteValue = (int) javaByte & 0xFF;
Java supports only signed integers. byte is integer number as well so Java is just consistent here.
This is not case for C for example where char type doesn't have defined whether it's signed or unsigned. You need to explicitly tell. This is in my opinion much worse than saying that it's by default signed.
Anyway, the original intention in C for char was a string character where the unsigned made sense. In Java there is different type for string character - char.

Converting Integer to Byte in Java [duplicate]

This question already has an answer here:
Promotion of byte to int or long
(1 answer)
Closed 7 years ago.
I have the following code:
int i =128;
byte b = (byte) i;
System.out.println( Integer.toBinaryString(i)); //10000000
System.out.println( Integer.toBinaryString(b)); //11111111111111111111111110000000
could someone explain why 1's were added to the left when casting from Integer to Byte and how could a byte carry more than 8 bits !?
You are calling .toBinaryString on the Integer class, so your number is treated as an Integer in any case.
The reason the second call has so many 1 is because it is a negative number. In Java, bytes are signed, so the maximum positive value is 127. By casting 128 into a byte you are actually representing -128. When you cast that small negative number into an 32 bit signed integer as you were doing, all those 1s appear at the beginning.

How to output the absolute value of an Unsigned integer in java [duplicate]

This question already has answers here:
Declaring an unsigned int in Java
(10 answers)
Closed 4 years ago.
I want to assign 4294967295 to a variable (2^32-1)
It is obvious that I can't do that with Integer, and can do it with Long.
However, I noted that Java 8 offers Unsigned Integers (at least some methods).
Does any one know what the method, Integer.parseUnsignedInt() does?
When I input "4294967295" to that, and print the variable, it gives the output as -1
(-2 for 4294967294, -3 for 4294967293 and so on...)
Is there a way that I can still have 4294967295 in a variable?
Am I missing something here?
a=Integer.parseUnsignedInt("4294967295");
System.out.println(a);
This gives the output as -1 but I expected 4294967295.
You can view that integer as unsigned int by calling toUnsignedString():
int uint = Integer.parseUnsignedInt("4294967295");
System.out.println(Integer.toUnsignedString(uint));
You can also call some other methods that interpret that int as unsigned.
For example :
long l = Integer.toUnsignedLong(uint);
System.out.println(l); // will print 4294967295
int x = Integer.parseUnsignedInt("4294967295");
int y = 5;
int cmp1 = Integer.compareUnsigned(x,y); // interprets x as 4294967295 (x>y)
int cmp2 = Integer.compare(x,y); // interprets x as -1 (x<y)
As far as I understand https://blogs.oracle.com/darcy/entry/unsigned_api, the unsigned support is not done by introducing a new type. The values are still stored in (signed) int variables but they provide methods to interpret the value as unsigned:
To avoid dealing with the overhead of boxed values and to allow reuse of the built-in arithmetic operators, the unsigned API support does not introduce new types like UnsignedInt with instance methods to perform addition, subtraction, etc. However, that lack of separate Java-level unsigned types does mean a programmer can accidentally improperly mix signed and unsigned values.
You used the "unsigned" version of parse, but you don't show which method you use to "print the variable". Probably you picked the (default) signed one.
Is there a way that I can still have 4294967295 in a varible?
Yes. Use long. (To me, it sounds like you're overthinking it.)

Can one force String.valueOf() to return unsigned value in Java? [duplicate]

This question already has answers here:
Can we make unsigned byte in Java
(17 answers)
Closed 9 years ago.
I have a byte array:
byte[] a = new byte[3];
which I have added some bytes. For this example, let's say 3, 4, and 210.
I would like to print this string of bytes to look like 3 4 210, but instead I get 3 4 -46
I am using String.valueOf(a[i]) to do my conversion. Is there any way to force this conversion to give unsigned values?
Thanks in advance,
EDIT: Thanks to the various feedback on this question. I had not realized Java Bytes were signed values by default, and so was suspecting the String.valueOf() method as being the issue. It turns out just simply using
String.valueOf(a[i]&0xFF)
takes care of the signed formatting issue.
Again, thank you for your feedback!
Guava provides a UnsignedBytes class that can make that conversion. The static toString(byte) method
Returns a string representation of x, where x is treated as unsigned.
For example
System.out.println(UnsignedBytes.toString(a[i]));
where a[i] = -46 would print
210
Internally, all this does is call
public static int toInt(byte value) {
return value & UNSIGNED_MASK; // UNSIGNED_MASK = 0xFF
}
and convert the int to a String which it returns.
For an explanation
With
someByte & 0xFF
since OxFF is an integer literal, the someByte value is widened to an int. Let's take for example the value -46. Its binary representation is
11111111111111111111111111010010
The binary representation of 0xFF is
11111111 // ie 255
if you and & the two
11111111111111111111111111010010
00000000000000000000000011111111
--------------------------------
00000000000000000000000011010010
which is equal to
210
Basically you only keep the lower 8 bits of the int.
Java byte data type range is minimum value of -128 and a maximum value of 127 (inclusive). String.valueOf(a[i]) doesn't do this conversion. Use int type instead.
byte
byte range limit is within -128 to 127,
so for 210 it gives -46. so convert it using int type
You've run into Java's famous problem of bytes treated as signed even though most of the real world prefers these unsigned. Try this:
int[] signedArr = new int[a.length];
for (int i=0; i<a.length; ++i) {
signedArr[i] = a[i] & 0xff;
}
Then you can work with signedArr.

Categories

Resources