How can I turn an int into three bytes in Java?

I am trying to convert an int into three bytes representing that int (big endian).

I'm sure it has something to do with bit-wise and and bit shifting. But I have no idea how to go about doing it.

For example:

int myInt;

// some code

byte b1, b2 , b3; // b1 is most significant, then b2 then b3.

*Note, I am aware that an int is 4 bytes and the three bytes have a chance of over/underflowing.


Asked by: Freddie170 | Posted: 23-01-2022






Answer 1

To get the least significant byte:

b3 = myInt & 0xFF;

The 2nd least significant byte:

b2 = (myInt >> 8) & 0xFF;

And the 3rd least significant byte:

b1 = (myInt >> 16) & 0xFF;

Explanation:

Bitwise ANDing a value with 0xFF (11111111 in binary) will return the least significant 8 bits (bits 0 to 7) in that number. Shifting the number to the right 8 times puts bits 8 to 15 into bit positions 0 to 7 so ANDing with 0xFF will return the second byte. Similarly, shifting the number to the right 16 times puts bits 16 to 23 into bit positions 0 to 7 so ANDing with 0xFF returns the 3rd byte.

Answered by: Carlos670 | Posted: 24-02-2022



Answer 2

byte b1 = (myint >> 16) & 0xff;
byte b2 = (myint >> 8) & 0xff;
byte b3 = myint & 0xff;

I am unsure how this holfds in java though, i aam not a java dev

Answered by: Rafael191 | Posted: 24-02-2022



Answer 3

An int doesn't fit into 3 bytes. However, assuming that you know these particular ones do:

   byte b1 = (myInt & 0xff);
   myInt >>= 8;
   byte b2 = (myInt & 0xff);
   myInt >>= 8;
   byte b3 = (myInt & 0xff);

Answered by: Max393 | Posted: 24-02-2022



Answer 4

In Java

int myInt = 1;
byte b1,b2,b3;
b3 = (byte)(myInt & 0xFF);
b2 = (byte)((myInt >> 8) & 0xFF);
b1 = (byte)((myInt >> 16) & 0xFF);
System.out.println(b1+" "+b2+" "+b3);

outputs 0 0 1

Answered by: Julian472 | Posted: 24-02-2022



Answer 5

The answer of Jeremy is correct in case of positive integer value. If the conversion should be correct for negative values, it is little more complicated due to two's-complement format (https://en.wikipedia.org/wiki/Two%27s_complement). The trick is to remove the gap between the interesting bits (the less significant bits) and the 'sign' bit. One easy method is multiplication of the number.

    int myIntMultiplied = myInt * 256;

    byte b1, b2, b3;

    b3 = (byte) ((myIntMultiplied >> 8) & 0xFF);
    b2 = (byte) ((myIntMultiplied >> 16) & 0xFF);
    b1 = (byte) ((myIntMultiplied >> 24) & 0xFF);

This will end up with correct two's-complement format for negative values.

PS: You can check and compare binary representation this way:

Integer.toBinaryString(myInt);
Integer.toBinaryString(myIntMultiplied );

Answered by: Lydia131 | Posted: 24-02-2022



Similar questions

A good library to do URL Query String manipulation in Java

Closed. This question does not meet Stack Overflow guid...


bit manipulation - Bitwise AND, Bitwise Inclusive OR question, in Java

I've a few lines of code within a project, that I can't see the value of... buffer[i] = (currentByte & 0x7F) | (currentByte & 0x80); It reads the filebuffer from a file, stored as bytes, and then transfers then to buffer[i] as shown, but I can't understand what the overall purpose is, any ideas? Thanks


bit manipulation - In Java, when using bitshifts, why does 1 << 32 != 1 << 31 << 1?

int a = 1 &lt;&lt; 32; int b = 1 &lt;&lt; 31 &lt;&lt; 1; Why does a == 1? b is 0 as I expected.


bit manipulation - getting the bottom 16 bits of a Java int as a signed 16-bit value

Hmmm. Consider this program, whose goal is to figure out the best way to get the bottom 16 bits of an integer, as a signed integer. public class SignExtend16 { public static int get16Bits(int x) { return (x &amp; 0xffff) - ((x &amp; 0x8000) &lt;&lt; 1); } public static int get16Bits0(int x) { return (int)(short)(x); } public static void main(String[] args)...


bit manipulation - In Java, is it possible to clear a bit?

In Java, is it possible to clear a bit using bitwise operations?


java - TextBox Search string manipulation

This is sort of complicated for me to explain,let me try anyway. i want to know how can i manipulate the string i've collected from this(such type of ) textbox and use it for my pupose in webapp or dektop app. alt text http://img35.imageshack.us/img35/1495/searchr.jpg i mean for example, i've two situations. ...


Final variable manipulation in Java

Could anyone please tell me what is the meaning of the following line in context of Java: final variable can still be manipulated unless it's immutable As far as I know, by declaring any variable as final, you can't change it again, then what they mean with the word immutable in above line?


testing - How to test file system manipulation in Java

I'm writing a program that creates and edits directories and files in a file system. I want to write a test that does things like (1) check if the given directory exists and (2) create a directory within the existing directory. What is the best way to test functionality like this? Is there a way to fake a file system (e.g. in memory?) or would I have to just use a temporary outp...


bit manipulation - How do I compare two longs as unsigned in Java?

I'm storing bit patterns of unsigned 64-bit numbers in a long variable and want to calculate the distance between two of them on the unsigned range. Because Java interprets long as a two's complement signed integer, I can't just do a - b, as the following example shows: // on the unsigned range, these numbers would be adjacent long a = 0x7fffffffffffffffL; long b = 0x8...


bit manipulation - Finding Highest Order 1 in a Java Primitive

I need to find the highest order 1 in some longs, ints, and shorts in Java. For example, if I had a char that looked like 00110101, I need a method that will return 2 (index of highest order 1). Now, I know that you can do this using a for loop like: for(int i=0; i&lt;8; i++) if((x &amp; 1&lt;&lt;i) != 0) return i; return -1; but this is way slower than what I want...






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top