Shift byte value into int - java

I am attempting to store a single byte value into a position within an int, however, I am having trouble figuring out how this would be done.
I have shifted the byte that i want to store in the int to the correct value, but I am at a loss as to how i then combine this with the value.
public static int ibyteToInt(int b, int pos, int val)
{
return ((b & 0xff) << (8 * pos)) ??;
}
The simplest solution would be to unpack the 3 other bytes from the int and then recombine the 4 bytes into an int but of course this would not be very performant.
public static int bytesToInt(byte a, byte b, byte c, byte d)
{
return ((a & 0xff) << 0) | ((b & 0xff) << 8) | ((c & 0xff) << 16) | ((d & 0xff) << 24);
}

An other simple solution is clearing out the target byte, then OR-ing in the new value:
int replaceByte(int value, byte b, int pos)
{
return (value & ~(0xFF << (pos * 8))) | ((b & 0xFF) << (pos * 8));
}
This is very similar to how to do the same thing in C#, but Java requires & 0xFF to prevent sign-extension of the byte. If the "byte" is a value between 0 and 255 and passed in as an int, that step is not required.

Related

Convert DWORD byte array to a unsigned long number

I want the unsigned value of a little-endian DWORD byte array.
This is what I wrote:
private long getUnsignedInt(byte[] data) {
long result = 0;
for (int i = 0; i < data.length; i++) {
result += (data[i] & 0xFF) << 8 * (data.length - 1 - i);
}
return result;
}
Is it right?
No I am afreaid that is big endian.
public long readUInt(byte[] data) {
// If you want to tackle different lengths for little endian:
//data = Arrays.copyOf(data, 4);
return ByteBuffer.wrap(data)
.order(ByteOrder.LITTLE_ENDIAN)
.getInt() & 0xFF_FF_FF_FFL;
}
The above does a 4 byte to (signed) int conversion and then make it unsigned.
Correted for BigEndian
If by DWORD you mean 32 bits unsigned int, try this
public long readUInt(byte[] data) {
return (
((long)(data[3] & 0xFF) << 24) |
((long)(data[2] & 0xFF) << 16) |
((long)(data[1] & 0xFF) << 8) |
((long)(data[0] & 0xFF) << 0));
}
Joop Eggen's answer is correct, I think this one is faster as there is no object allocation.

Java: custom to byte conversions

I am working with some low capacity module and I need to compress the data as much as possible. The data will look like this:
DeviceEvent:
1 byte:
2 bits for status (00 each time)
6 bits for rgb color (3 x 2 bits)
2 bytes: number of minutes from now to a certain datetime
I need to create a constructor (preferably 2 constructors) for conversion from/to:
Event:
byte[] color (rgb, colors will get simplified to only 64 available)
some datetime (but I will get the integer for difference in minutes and it will be small enough to fit in two bits)
So basically I need:
byte[3] color <-> 1 byte status and color
int minutes <-> byte[2]
minutes
I will be thankful for any help
I'm not very sure what is your problem, probably this will help:
final byte red = 1; // 01 binary
final byte green = 2; // 10 binary
final byte blue = 3; // 11 binary
final byte finalColor = (byte) ((red & 0x3) << 4) | ((green & 0x3) << 2) | (blue & 0x3);
System.out.println(finalColor);// finalColor is 011011 = 27 decimal
final int minutes = 0x1234; // first byte is 0x12, second byte is 0x34
final byte[] bytes = {(byte) (((minutes) >>> 8) & 0xff), (byte) (minutes & 0xff)};
System.out.println(bytes[0]); // 0x12 = 18 decimal
System.out.println(bytes[1]); // 0x34 = 52 decimal
I am not sure what the second problem is. So I made these two functions that might help you:
public static int convertToInt(int a, int b, int c, int d) {
a = Math.min(a, 255);
b = Math.min(b, 255);
c = Math.min(c, 255);
d = Math.min(d, 255);
return ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | (d & 0xFF);
}
public static int[] extractComponents(int data) {
int a = (data >> 24) & 0xFF;
int b = (data >> 16) & 0xFF;
int c = (data >> 8) & 0xFF;
int d = data & 0xFF;
return new int[] {a, b, c, d};
}
The convertToInt function takes four numbers(that are less than 255) and puts them all in one int.
The extractComponents function does the opposite.
This is an example:
int data = 0xC8E0601B;
int[] dataA = extractComponents(data);
for(int i = 0 ; i < dataA.length; i++) System.out.printf("%x\n", dataA[i]);
System.out.printf("%x\n", convertToInt(dataA[0], dataA[1], dataA[2], dataA[3]));

How to convert Java double to byte[], and byte[] to double (IEEE 754 double-precision binary floating-point format)

I have 3 methods (1 functional (double to byte[]), and one returning unexpected value (byte[] to double), and 1 method that is functional but performs to many operations to use Hex to double)).
Performance is the utmost importance, so if you have more efficient code, please share.
Functional method converts from double to byte[] getFloat64(11.27d) returns byte[]=hex string "40268A3D70A3D70A":
public static byte[] getFloat64(double value)
{
final byte[] float64Bytes = new byte[8];
long double64Long=Double.doubleToLongBits(value);
float64Bytes[0] = (byte)((double64Long >> 56) & 0xff);
float64Bytes[1] = (byte)((double64Long >> 48) & 0xff);
float64Bytes[2] = (byte)((double64Long >> 40) & 0xff);
float64Bytes[3] = (byte)((double64Long >> 32) & 0xff);
float64Bytes[4] = (byte)((double64Long >> 24) & 0xff);
float64Bytes[5] = (byte)((double64Long >> 16) & 0xff);
float64Bytes[6] = (byte)((double64Long >> 8) & 0xff);
float64Bytes[7] = (byte)((double64Long >> 0) & 0xff);
return float64Bytes;
}
Incorrect double value is returned from this byte[] to double method (calling getFloat64(getFloat64(11.27d)) returns 9.338087023E-315):
public static double getFloat64(byte[] bytes)
{
return Double.longBitsToDouble((long)((bytes[0] & 0xFF) << 56)
| ((bytes[1] & 0xFF) << 48)
| ((bytes[2] & 0xFF) << 40)
| ((bytes[3] & 0xFF) << 32)
| ((bytes[4] & 0xFF) << 24)
| ((bytes[5] & 0xFF) << 16)
| ((bytes[6] & 0xFF) << 8)
| ((bytes[7] & 0xFF) << 0));
}
The final method returns the correct answer calling getFloat64("40268A3D70A3D70A") returns 11.27:
public double getFloat64(String hex_double)
{
long longBits = Long.valueOf(hex_double,16).longValue();
return Double.longBitsToDouble(longBits);
}
What is wrong with the middle method? Why does it not behave like the last method by returning 11.27?
The problem is that (bytes[0] & 0xFF) is still a 32-bit integer value. If you bit-shift it 56 bits to the left on a 32-bit value, Java shifts by 56 % 32 = 24 bits instead of 56 bits.
You first need to promote the value to 64-bit long before bit-shifting it. One way to do that is to & with a long value (0xFFL). Any integral number-literal (that ordinarily has the type int and hence 32 bits) can be turned into a long-literal by appending L or l to it.
Corrected code:
public static double getFloat64(byte[] bytes)
{
return Double.longBitsToDouble(((bytes[0] & 0xFFL) << 56)
| ((bytes[1] & 0xFFL) << 48)
| ((bytes[2] & 0xFFL) << 40)
| ((bytes[3] & 0xFFL) << 32)
| ((bytes[4] & 0xFFL) << 24)
| ((bytes[5] & 0xFFL) << 16)
| ((bytes[6] & 0xFFL) << 8)
| ((bytes[7] & 0xFFL) << 0));
}
ob-JLS reference: Java Language Specification 15.9:
If the promoted type of the left-hand operand is int, only the five
lowest-order bits of the right-hand operand are used as the shift
distance. It is as if the right-hand operand were subjected to a
bitwise logical AND operator & (ยง15.22.1) with the mask value 0x1f
(0b11111). The shift distance actually used is therefore always in the
range 0 to 31, inclusive.

Convert 3 bytes array to number

I convert a short number to a 3 byte array using the following code:
static byte[] convertTo3ByteArray(short s) {
byte[] ret = new byte[3];
ret[0] = (byte) (s & 0xff);
ret[1] = (byte) ((s >> 8) & 0xff);
ret[2] = (byte) (0x00);
return ret;
}
This works very well.
I found a code on Stackoverflow to convert the array back to a number:
static int convertToInt(byte[] b) {
return ((b[0] << 0) | (b[1] << 8) | (b[2] << 16));
}
And when I convert 258 to byte array, and then use this code, it returns 258.
But for number 675, this code returns -93.
How do I have to change the convertToShort method to get 675 back?
I suppose it has something to do with bitshift and loss of data? Or signed bytes?
Try with this modified method:
static int convertToShort(byte[] b) {
return (((b[0] & 0xFF) << 0) | ((b[1] & 0xFF) << 8) | ((b[2] & 0xFF) << 16));
}
In the array some bytes are negative, you need to convert them back to "positive values" with byteVal & 0xFF before doing the bit shift
A short has 16 bits of information, so that would be two bytes. When you try to store a third byte with | (b[2] << 16), it would go off the end of the short's bits, which is a problem. I.e. you can't do what you want to.
Changing to using a char datatype will fix this issue as they are the only unsigned type in Java:
https://stackoverflow.com/a/21089624/1590490
static char[] convertTo3ByteArray(short s) {
char[] ret = new char[3];
ret[0] = (char) (s & 0xff);
ret[1] = (char) ((s >> 8) & 0xff);
ret[2] = (char) (0x00);
return ret;
}
static int convertToShort(char[] b) {
return ((b[0]) | (b[1] << 8) | (b[2] << 16)); // the original << 0 shift does nothing
}

Converting int to byte in Android

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)
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);
}
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;
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.
for 0-255 numbers.
int i = 200; // your int variable
byte b = (byte)(i & 0xFF);

Categories

Resources