This question already has answers here:
Print an integer in binary format in Java
(24 answers)
Closed 8 years ago.
Say I have an integer number: 11728322732
how can i convert it to a string according to its byte representation, i.e 11100011000000001100000111110001 (32 bits \ 4 bytes \ integer)
Thanks
Here it is:
Long.toBinaryString(11728322732L);
Actually, 11728322732 is not an Integer but a Long (because it is greater than Integer.MAX_VALUE). So if you really want to convert this long to a 32-bits int (can't figure why actually), you could do:
Integer.toBinaryString((int)11728322732L);
Related
This question already has answers here:
How do you specify a byte literal in Java?
(6 answers)
How to specify a constant is a byte or short?
(3 answers)
Java binary literals - Value -128 for byte
(1 answer)
Closed 4 months ago.
So to convert a positive binary number into a negative one I have to invert all of the bits of the number and add 1.
`byte x = 0b0111_1111;
System.out.println(x); // 127
x = 0b1111_1111_1111_1111_1111_1111_1000_0001;
System.out.println(x); // -127
I don't understand why I have to expand my number to 32 bit form by adding zeros on the left side before reversing the bits, why exactly to 32 bit form?
This question already has answers here:
Why can't I assign a 'long' a value of 4 billion? [duplicate]
(6 answers)
The literal xyz of type int is out of range
(5 answers)
Closed 5 years ago.
I am trying to test the difference between int and long data types and I've learned that int has 32 bits whereas long has 64 bits. According to this, long data type's maximum value is 9,223,372,036,854,775,807. I am only using a 13-digit number however I am getting this error:
error: integer number too large: 2147483645234
long exceedLong = 2147483645234;
I thought a long variable should be able to handle that value. I'm using JDK and JRE version 8 and I'm compiling and running the code in command prompt, using javac and java.
This question already has answers here:
Datatype to store 20 digit number
(4 answers)
Closed 6 years ago.
I am trying to convert 10000000000000000000000000000000 to BigInteger.
But it is not converting.
My code is
BigInteger number = BigInteger.valueOf(10000000000000000000000000000000);
It is showing The literal 10000000000000000000000000000000 of type int is out of range.
Is there any way to convert this number as I have to use this number as integer in my programme?
The whole point of BigInteger is to support numbers long and int cannot so you can't use those. What you can do is use a String.
BigInteger bi = new BigInteger("10000000000000000000000000000000"):
This question already has answers here:
Convert integer into byte array (Java)
(11 answers)
Closed 8 years ago.
Suppose i have the number 520 that is mapped in two bytes that gives me the number: 1000001000 and i want to convert this number (520) to two other numbers, these numbers should be: 2 and 8 because 00000010 will give me 2 and 00001000 will give me 8. how can i do this with java?
Like this:
int theNumber = 520;
byte oneNumber = (byte)theNumber;
byte otherNumber = (byte)(theNumber >> 8);
This question already has answers here:
Java code To convert byte to Hexadecimal
(23 answers)
Closed 9 years ago.
I have a MAC address represented as a byte[] in Java and want it as a hexadecimal string, as they are usually represented. How can I do this with as easy as possible?
The byte array has length 6.
Try this:
String hexValue = String.format("%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);