Converting from byte to int in Java - java

I have generated a secure random number, and put its value into a byte. Here is my code.
SecureRandom ranGen = new SecureRandom();
byte[] rno = new byte[4];
ranGen.nextBytes(rno);
int i = rno[0].intValue();
But I am getting an error :
byte cannot be dereferenced

Your array is of byte primitives, but you're trying to call a method on them.
You don't need to do anything explicit to convert a byte to an int, just:
int i=rno[0];
...since it's not a downcast.
Note that the default behavior of byte-to-int conversion is to preserve the sign of the value (remember byte is a signed type in Java). So for instance:
byte b1 = -100;
int i1 = b1;
System.out.println(i1); // -100
If you were thinking of the byte as unsigned (156) rather than signed (-100), as of Java 8 there's Byte.toUnsignedInt:
byte b2 = -100; // Or `= (byte)156;`
int = Byte.toUnsignedInt(b2);
System.out.println(i2); // 156
Prior to Java 8, to get the equivalent value in the int you'd need to mask off the sign bits:
byte b2 = -100; // Or `= (byte)156;`
int i2 = (b2 & 0xFF);
System.out.println(i2); // 156
Just for completeness #1: If you did want to use the various methods of Byte for some reason (you don't need to here), you could use a boxing conversion:
Byte b = rno[0]; // Boxing conversion converts `byte` to `Byte`
int i = b.intValue();
Or the Byte constructor:
Byte b = new Byte(rno[0]);
int i = b.intValue();
But again, you don't need that here.
Just for completeness #2: If it were a downcast (e.g., if you were trying to convert an int to a byte), all you need is a cast:
int i;
byte b;
i = 5;
b = (byte)i;
This assures the compiler that you know it's a downcast, so you don't get the "Possible loss of precision" error.

byte b = (byte)0xC8;
int v1 = b; // v1 is -56 (0xFFFFFFC8)
int v2 = b & 0xFF // v2 is 200 (0x000000C8)
Most of the time v2 is the way you really need.

if you want to combine the 4 bytes into a single int you need to do
int i= (rno[0]<<24)&0xff000000|
(rno[1]<<16)&0x00ff0000|
(rno[2]<< 8)&0x0000ff00|
(rno[3]<< 0)&0x000000ff;
I use 3 special operators | is the bitwise logical OR & is the logical AND and << is the left shift
in essence I combine the 4 8-bit bytes into a single 32 bit int by shifting the bytes in place and ORing them together
I also ensure any sign promotion won't affect the result with & 0xff

Primitive data types (such as byte) don't have methods in java, but you can directly do:
int i=rno[0];

Bytes are transparently converted to ints.
Just say
int i= rno[0];

I thought it would be:
byte b = (byte)255;
int i = b &255;

int b = Byte.toUnsignedInt(a);

Related

Can we convert int to Byte using wrapper class, without type casting?

I was learning wrapper class, here I learned how to convert int to Interger wrapper class.
But I want to convert int to Byte using Byte wrapper class.
I have tried
int a =10;
Byte c = Byte; //(Not getting suggestion in eclipse)
for example, I know how to convert int to Interger refer code below.
int a =10;
Integer b = Integer.valueOf(a);
You could add an additional cast to the integer like this:
int a = 10;
Byte c = Byte.valueOf((byte)a);
Try it online.
Btw, going from a primitive to an Boxed value is done implicitly by the compiler. So the code above doesn't necessary need the .valueOf:
int a = 10;
Byte c = (byte)a;
Try it online.
But, when you're explicitly casting values, always keep in mind that it can hold unexpected results when doing it wrong. In this case for example, the size of a a byte is smaller than an integer (the range of a byte is [-128, 127] and of an int is [-2147483648, 2147483647]), so something like this wouldn't give the expected result:
int a = 200;
Byte c = Byte.valueOf((byte)a); // Results in -56
Try it online.
This is also the reason why you usually can't go to a smaller type, since it might not fit, but you can go to a larger one without needing the cast:
byte a = 10;
Integer c = Integer.valueOf(a);
Try it online.
int x=30;
Byte c = (byte)x; // Java has auto boxing for wrapper class Byte to byte and opposite automatically
You can cast the same way you normally do with Integer wrapper.
int a =10;
Byte b = Byte.valueOf((byte)a);
//b = 10
An int is 4 bytes. So you can't convert an integer to a byte without losing some data.
int a =1000;
Byte b = Byte.valueOf((byte)a);
//b = -24
You can, however, convert an int to an array of bytes.
Java - Convert int to Byte Array of 4 Bytes?
Same you can do for Byte too:
1. autoboxing
Byte b = (byte) a;
2. valueOf(byte b)
Byte b = Byte.valueOf((byte)a)
Returns a Byte instance representing the specified byte value. If a new Byte instance is not required, this method should generally be used in preference to the constructor Byte(byte), as this method is likely to yield significantly better space and time performance since all byte values are cached.
From JLS 5.0-2. Conversions In Various Contexts so you can't perform 5.1.3. Narrowing Primitive Conversion without cast operator.
// Casting conversion (5.4) of a float literal to
// type int. Without the cast operator, this would
// be a compile-time error, because this is a
// narrowing conversion (5.1.3):
int i = (int)12.5f;

Int cannot be converted to bytes using java

I am trying to convert my integer value to bytes which is to unsigned bytes. I heard that java bytes only provide until 127. Then I tried to reedit my code
This is my code after editing:
Integer servo1 = 5117;
Byte data1;
data1 = (((byte)servo1) >> 6 & (byte)0xFF);
What if I want to add a byte inside a byte array is it possible?
This is the code:
send = {(byte)0xE1, data1, data2, data3};
Now the error is showing Integer cannot be converted to Byte Can I know how to solve this problem. Thanks
Your syntax is not quite right. You want to cast the whole expression on the right side to byte if you want to assign it to that variable type. So use this:
Integer servo1 = 5117;
Byte data1;
data1 = (byte) ((servo1) >> 6 & 0xFF);
You may be interested in the Java article on type conversions (specifically the section on integer promotions): https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html
The error you are getting is **Integer** cannot be converted to **Byte**. It is because explicit conversion between numerical values cannot be done by their Wrapper classes. By (byte)servo1 you are trying to convert a Integer i.e. a wrapper class of int into byte i.e. a primitive type.
Consider the following example :
Long l = 10000000L;
Integer i = (int) l; // Long cannot be converted to int
Byte b = (byte) i; // Integer cannot be converted to byte
To do conversion between wrapper classes you should use methods provided by them :
Long l = 10000000L;
Integer i = l.intValue();
Byte b = i.byteValue();
And to do conversion between primitive primitive type you can do it explicitly as you were trying to do with wrapper classes :
long l = 1000000L;
int i = (int) l;
byte b = (byte) b;
That means your conversion would have worked if you have used primitive types :
int servo1 = 5117;
byte data1;
data1 = (((byte)servo1) >> 6 & (byte)0xFF);
Or if you have used correct method to convert wrapper classes you were using :
Integer servo1 = 5117;
Byte data1;
data1 = (byte)(servo1.byteValue() >> 6 & (byte) 0xFF);

Reading serial information in Java

I'm working on porting an Android app that has already been developed in Python. In the Python program, there is a line that I'm trying to fully understand:
self.comfd = Serial(...) # from the pySerial API
....
self.buffer = list(struct.unpack('192H', self.comfd.read(384)))
From what I understand, self.comfd.read(384) is reading 384 bytes, and the unpack('192H' is unpacking 192 unsigned shorts from that data. Is this correct?
Now in Java, I've been able to read the buffer using
SerialPort device = SerialPort(file, baud, flags);
InputStream in = device.getInputStream();
My question is, now that I have the input stream, how do I create the unsigned shorts like the Python program is doing?
What I've tried (not producing correct values):
byte[] buffer = new byte[384];
in.read(buffer);
ByteBuffer bb = ByteBuffer.allocate(2);
for (int i = 0; i < buffer.length / 2; i++) {
bb.put(buffer[i]);
bb.put(buffer[i + 1]);
short val = bb.getShort(0);
System.out.println(val);
bb.clear();
}
What am I doing wrong? Thanks for any help!
edit: I incorporated Jason C's answer and also I was looping incorrectly. By changing it to
for (int i = 0; i < buffer.length; i=i+2) that fixed my problem.
You could use a char (it's a 16-bit unsigned value in Java), e.g.:
byte[] buffer = ...;
ByteBuffer bb = ByteBuffer.wrap(buffer); // don't need to put()
int val = (int)bb.getChar(0);
Use bb.order() to set big- vs. little-endian.
You can also just pack the 2 bytes into an int (assuming little-endian) without using a ByteBuffer. Byte is signed in Java, so you will have to convert the byte to an unsigned value before shifting, which you can do by temporarily storing it in a short (or an int or anything large enough to hold 0-255):
short b0 = (buffer[0] & 255); // trick converts to unsigned
short b1 = (buffer[1] & 255);
int val = b0 | (b1 << 8);
// or just put it all inline:
int val = (buffer[0]&255) | ((buffer[1]&255) << 8);
For big-endian data just swap b0 and b1.
Hope that helps.
Java has no unsigned numbers (char is 16bit unsigned but it's not a number and math with char will always result in implicit casts to int)
If you read 2 bytes of unsigned data into a short and want to see values in range from 0-65535 (instead of -32768 - 32767) you'll have to use a type that can have values in that range.
In case of 16bit short next bigger one is 32bit int. The conversion that does the trick is
short signed = ...;
int unsigned = signed & 0xFFFF;
Assuming signed has a value of 0xFFFF this is what happens:
short signed = -1; // FFFF on byte level
The expression signed & 0xFFFF contains a short and an int. 0xFFFF is a literal integer type number which when found in Java source is considered int. You could make it long by changing it to 0xFFFFL (you would need that if you want to convert unsigned int to long).
Since the & operator needs both sides in a common type Java will silently convert the smaller one.
int stillSigned = (int) signed; // hidden step
It will still have the exact same value of -1 since that's what it was before when looking at it unsigned but it is changed on bytelevel to 0xFFFFFFFF.
Now the bit-manipulation is applied to remove all the added FFs
int unsigned = stillSigned & 0xFFFF;
and you end up with 0x0000FFFF on byte level and can finally see the value of 65535.
Since you happen to have 16bit values you can use char and simply cast it to int.
char value = ...;
int unsigned = value;
But above approach works for any unsigned conversion: byteValue & 0xFF, shortValue & 0xFFFF, intValue & 0xFFFFFFFFL
The next thing you should do is not to use a simple InputStream to do
SerialPort device = SerialPort(file, baud, flags);
InputStream in = device.getInputStream();
byte[] buffer = new byte[384];
in.read(buffer);
Reason is that InputStream#read(byte[]) is not guaranteed to read all the bytes you want in your buffer. It returns you the number of bytes it has read or -1 if the stream is done. Manually writing code that ensures you have a filled buffer is nasty but there is a simple solution: DataInputStream
SerialPort device = SerialPort(file, baud, flags);
DataInputStream in = new DataInputStream(device.getInputStream());
byte[] buffer = new byte[384];
in.readFully(buffer);
DataInputStream has very nice functionality that you could use:
SerialPort device = SerialPort(file, baud, flags);
DataInputStream in = new DataInputStream(device.getInputStream());
int unsignedShort = in.readUnsignedShort();
Another way to get different numbers out of byte[] data is to use ByteBuffer since that provides methods like .getShort()
SerialPort device = SerialPort(file, baud, flags);
DataInputStream in = new DataInputStream(device.getInputStream());
byte[] buffer = new byte[384];
in.readFully(buffer);
ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
while (byteBuffer.hasRemaining()) {
int unsigned = byteBuffer.getChar();
System.out.println(unsigned);
}

Byte typecasting in java

the program gives me loss of precision error but i cant think of any precision loss since numbers are small
This is the code ##
class Demo
{
public static void main(String args[])
{
byte b1=3;
byte b2=2;
byte b3=b1+b2;
System.out.println(b3);
}
}
The addition expression b1 + b2 is of type int - there aren't any addition operators defined on smaller types than int. So in order to convert that back to a byte, you have to cast:
byte b3 = (byte) (b1 + b2);
Note that although you happen to know that the values are small, the compiler doesn't care about the values you've set in the previous two lines - they could both be 100 for all it knows. Likewise although you know that the int you're trying to assign to a byte variable is only the result of adding two byte values together (or rather, two values promoted from byte to int), the expression as a whole is just int and could have come from anywhere as far as the language is concerned.
(The fact that the addition can overflow is a separate matter, but that would be an inconsistent argument - after all, you can add two int values together and store the result in an int variable, even though the addition could have overflowed easily.)
Because 127 is the last value of byte.
Assume b1 = 127 and b2 = 2
now what happends b = b1+b2 = 129 [which is out of byte range, i.e. it's in int range]
now if you cast it b = (byte)(b1+b2), you will get -127 this is due to rounding of the value to byte.
byte b1=3;
byte b2=2;
byte b3=b1+b2; // you can't use byte here, Every time addition will result
int value
Because, If you trying to cast an int which is larger than byte range, there is a loss part of that value. So addition will not allow to use byte here.
when ever you do +,-,*,/,%
java internally uses a function
ex: max(int,dataType of operand 1,dataType of operand 2);
here in your code
max(int, byte,byte) ==> which one is bigger ? ==> int is bigger
so you may get POSSIBLE LOSS OF PRESSION
found :int
required : byte
Another Example
short a =10;
byte b=20;
short c = a+b;
now:
internally: max(int,OP1,OP2);
ie max(int,short,byte) ==> which is bigger Data Type? int
so java excepts int
int c = a+b; (works fine)
Another Example :
long a =10;
byte b=20;
short c = a+b;
now:
internally: max(int,OP1,OP2);
ie max(int,long,byte) ==> which is bigger Data Type? long
so java excepts long
long c = a+b;(works fine)
Another Example:
byte b = 10;
b = b+1;
now,
max(int,byte)==> which is bigger Data Type ? int
so,
int c = b+1;(works fine) or b = (byte) b+1;
hope you understand

How to Convert Int to Unsigned Byte and Back

I need to convert a number into an unsigned byte. The number is always less than or equal to 255, and so it will fit in one byte.
I also need to convert that byte back into that number. How would I do that in Java? I've tried several ways and none work. Here's what I'm trying to do now:
int size = 5;
// Convert size int to binary
String sizeStr = Integer.toString(size);
byte binaryByte = Byte.valueOf(sizeStr);
and now to convert that byte back into the number:
Byte test = new Byte(binaryByte);
int msgSize = test.intValue();
Clearly, this does not work. For some reason, it always converts the number into 65. Any suggestions?
A byte is always signed in Java. You may get its unsigned value by binary-anding it with 0xFF, though:
int i = 234;
byte b = (byte) i;
System.out.println(b); // -22
int i2 = b & 0xFF;
System.out.println(i2); // 234
Java 8 provides Byte.toUnsignedInt to convert byte to int by unsigned conversion. In Oracle's JDK this is simply implemented as return ((int) x) & 0xff; because HotSpot already understands how to optimize this pattern, but it could be intrinsified on other VMs. More importantly, no prior knowledge is needed to understand what a call to toUnsignedInt(foo) does.
In total, Java 8 provides methods to convert byte and short to unsigned int and long, and int to unsigned long. A method to convert byte to unsigned short was deliberately omitted because the JVM only provides arithmetic on int and long anyway.
To convert an int back to a byte, just use a cast: (byte)someInt. The resulting narrowing primitive conversion will discard all but the last 8 bits.
If you just need to convert an expected 8-bit value from a signed int to an unsigned value, you can use simple bit shifting:
int signed = -119; // 11111111 11111111 11111111 10001001
/**
* Use unsigned right shift operator to drop unset bits in positions 8-31
*/
int psuedoUnsigned = (signed << 24) >>> 24; // 00000000 00000000 00000000 10001001 -> 137 base 10
/**
* Convert back to signed by using the sign-extension properties of the right shift operator
*/
int backToSigned = (psuedoUnsigned << 24) >> 24; // back to original bit pattern
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
If using something other than int as the base type, you'll obviously need to adjust the shift amount: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Also, bear in mind that you can't use byte type, doing so will result in a signed value as mentioned by other answerers. The smallest primitive type you could use to represent an 8-bit unsigned value would be a short.
Except char, every other numerical data type in Java are signed.
As said in a previous answer, you can get the unsigned value by performing an and operation with 0xFF. In this answer, I'm going to explain how it happens.
int i = 234;
byte b = (byte) i;
System.out.println(b); // -22
int i2 = b & 0xFF;
// This is like casting b to int and perform and operation with 0xFF
System.out.println(i2); // 234
If your machine is 32-bit, then the int data type needs 32-bits to store values. byte needs only 8-bits.
The int variable i is represented in the memory as follows (as a 32-bit integer).
0{24}11101010
Then the byte variable b is represented as:
11101010
As bytes are signed, this value represent -22. (Search for 2's complement to learn more about how to represent negative integers in memory)
Then if you cast is to int it will still be -22 because casting preserves the sign of a number.
1{24}11101010
The the casted 32-bit value of b perform and operation with 0xFF.
1{24}11101010 & 0{24}11111111
=0{24}11101010
Then you get 234 as the answer.
The solution works fine (thanks!), but if you want to avoid casting and leave the low level work to the JDK, you can use a DataOutputStream to write your int's and a DataInputStream to read them back in. They are automatically treated as unsigned bytes then:
For converting int's to binary bytes;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
int val = 250;
dos.write(byteVal);
...
dos.flush();
Reading them back in:
// important to use a (non-Unicode!) encoding like US_ASCII or ISO-8859-1,
// i.e., one that uses one byte per character
ByteArrayInputStream bis = new ByteArrayInputStream(
bos.toString("ISO-8859-1").getBytes("ISO-8859-1"));
DataInputStream dis = new DataInputStream(bis);
int byteVal = dis.readUnsignedByte();
Esp. useful for handling binary data formats (e.g. flat message formats, etc.)
The Integer.toString(size) call converts into the char representation of your integer, i.e. the char '5'. The ASCII representation of that character is the value 65.
You need to parse the string back to an integer value first, e.g. by using Integer.parseInt, to get back the original int value.
As a bottom line, for a signed/unsigned conversion, it is best to leave String out of the picture and use bit manipulation as #JB suggests.
Even though it's too late, I'd like to give my input on this as it might clarify why the solution given by JB Nizet works. I stumbled upon this little problem working on a byte parser and to string conversion myself.
When you copy from a bigger size integral type to a smaller size integral type as this java doc says this happens:
https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3
A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value.
You can be sure that a byte is an integral type as this java doc says
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
byte: The byte data type is an 8-bit signed two's complement integer.
So in the case of casting an integer(32 bits) to a byte(8 bits), you just copy the last (least significant 8 bits) of that integer to the given byte variable.
int a = 128;
byte b = (byte)a; // Last 8 bits gets copied
System.out.println(b); // -128
Second part of the story involves how Java unary and binary operators promote operands.
https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2
Widening primitive conversion (ยง5.1.2) is applied to convert either or both operands as specified by the following rules:
If either operand is of type double, the other is converted to double.
Otherwise, if either operand is of type float, the other is converted to float.
Otherwise, if either operand is of type long, the other is converted to long.
Otherwise, both operands are converted to type int.
Rest assured, if you are working with integral type int and/or lower it'll be promoted to an int.
// byte b(0x80) gets promoted to int (0xFF80) by the & operator and then
// 0xFF80 & 0xFF (0xFF translates to 0x00FF) bitwise operation yields
// 0x0080
a = b & 0xFF;
System.out.println(a); // 128
I scratched my head around this too :). There is a good answer for this here by rgettman.
Bitwise operators in java only for integer and long?
If you want to use the primitive wrapper classes, this will work, but all java types are signed by default.
public static void main(String[] args) {
Integer i=5;
Byte b = Byte.valueOf(i+""); //converts i to String and calls Byte.valueOf()
System.out.println(b);
System.out.println(Integer.valueOf(b));
}
In terms of readability, I favor Guava's:
UnsignedBytes.checkedCast(long) to convert a signed number to an unsigned byte.
UnsignedBytes.toInt(byte) to convert an unsigned byte to a signed int.
Handling bytes and unsigned integers with BigInteger:
byte[] b = ... // your integer in big-endian
BigInteger ui = new BigInteger(b) // let BigInteger do the work
int i = ui.intValue() // unsigned value assigned to i
in java 7
public class Main {
public static void main(String[] args) {
byte b = -2;
int i = 0 ;
i = ( b & 0b1111_1111 ) ;
System.err.println(i);
}
}
result : 254
I have tested it and understood it.
In Java, the byte is signed, so 234 in one signed byte is -22, in binary, it is "11101010", signed bit has a "1", so with negative's presentation 2's complement, it becomes -22.
And operate with 0xFF, cast 234 to 2 byte signed(32 bit), keep all bit unchanged.
I use String to solve this:
int a = 14206;
byte[] b = String.valueOf(a).getBytes();
String c = new String(b);
System.out.println(Integer.valueOf(c));
and output is 14206.

Categories

Resources