This question already has answers here:
Print an integer in binary format in Java
(24 answers)
Closed 9 years ago.
How to get all 64 bits of a long as a String in Java?
So I want to do something like this -
long value = 10;
String bits = getBits(value);
System.out.println(bits);
I suppose the output would be
0000...1010 (64 bits)
And no, this is not homework! :)
Use Long.toString with the radix:
String bits = Long.toString(someLong, 2);
2 specifies binary as opposed to any other base.
Edit: If you want to left-pad:
String bits = Long.toString(someLong, 2);
StringBuilder sb = new StringBuilder();
for (int toPrepend=10-str.length(); toPrepend>0; toPrepend--) {
sb.append('0');
}
sb.append(bits);
String output = sb.toString();
You can call the method for it:
Long.toBinaryString(long number)
Related
This question already has answers here:
Binary presentation of negative integer in Java
(5 answers)
Closed 4 years ago.
I'm trying to negate(a.k.a invert) all bits of given int.
Lets say the given number's(given number is 5) binary representation is 101, and its negation (my output) should be 010.
I'm using ~ for each and every bit from least significant bit to most significant bit, to negate it.
public static void main (String[] args) throws java.lang.Exception
{
// your code go
int num = 5;
String givenNumInBinary = Integer.toBinaryString(num);
StringBuffer output = new StringBuffer();
for(int i = 0; i <= givenNumInBinary.length()-1;i++){
int msb = Character.getNumericValue(givenNumInBinary.charAt(i));
output.append(~msb);
}
System.out.println(output.toString());
}
My output turns out to be
-2-1-2
Why is that? What am I doing wrong?
Because you are inversing each digit at
int msb = Character.getNumericValue(givenNumInBinary.charAt(i));
output.append(~msb);
rather than inversing each bit.
Alternate solution would be
output.append(msb == 0 ? 1 : 0);
....
System.out.println(output.toString());
output
010
This question already has answers here:
How do I convert from int to String?
(20 answers)
How to Convert an int to a String? [duplicate]
(6 answers)
Java - Convert integer to string [duplicate]
(6 answers)
Closed 4 years ago.
How can I store a int value in a String?
For example:
int number = 10;
string word = number;
You can use the static method:
String.valueOf(number)
Or
new Integer(number).toString();
int number = 10;
string word = Integer.toString(number);
this will convert the integer "number" to string to be able to store it inside a string without changing it's value
just that easy, good luck :)
There are multiple ways to convert an int to string.
String word = Integer.toString(number);
String word = String.valueOf(number);
String word = new Integer(number).toString();
String word = String.format ("%d", number);
etc..
There are more ways to do this. But I prefer the 1st one.
Just do some googling you will find more answers! :)
Normal ways would be Integer.toString(number) or String.valueOf(number).
But, You can try this:
int number = 10;
String str = String.valueOf(number);
Or
StringBuilder sb = new StringBuilder();
sb.append("");
sb.append(number);
String str = sb.toString();
This question already has answers here:
Left padding a String with Zeros [duplicate]
(20 answers)
Closed 5 years ago.
How do I format a parsed String?
For example:
ph = "0412456839";
n = Long.parseLong(ph);
When I print n, I get (Without 0):
412456839
I want to print the same as ph. I could always do
System.out.println("0"+n);
but is there a way to format it?
Using java.util.Formatter
System.out.printf("%010d\n", n);
% 0 10 d
^ ^ ^
| | decimal
| width
zero-pad
If you need the output in a String instead of writing to System.out, use
String s = String.format("%010d", n);
You could create a DecimalFormat. That would allow you to format a number into a string using your own formatting rules. In this case, it is formatting it to be a 10 digit number that includes preceding zeros.
String ph = "0412456839";
Long n = Long.parseLong(ph);
String formattedN = new DecimalFormat("0000000000").format(n);
System.out.println(formattedN);
if you are using Apache Commons, you can use:
int paddingLength = 10;
String paddingCharacter = "0";
StringUtils.leftPad("" + n, paddingLength, paddingCharacter);
This question already has answers here:
How to Convert an int to a String? [duplicate]
(6 answers)
Closed 8 years ago.
I'm trying to create a method which generates a 4 digit integer and stores it in a string.
The 4 digit integer must lie between 1000 and below 10000. Then the value must be stored to PINString.
Heres what I have so far. I get the error Cannot invoke toString(String) on the primitive type int. How can I fix it?
public void generatePIN()
{
//generate a 4 digit integer 1000 <10000
int randomPIN = (int)(Math.random()*9000)+1000;
//Store integer in a string
randomPIN.toString(PINString);
}
You want to use PINString = String.valueOf(randomPIN);
Make a String variable, concat the generated int value in it:
int randomPIN = (int)(Math.random()*9000)+1000;
String val = ""+randomPIN;
OR even more simple
String val = ""+((int)(Math.random()*9000)+1000);
Can't get any more simple than this ;)
randomPIN is a primitive datatype.
If you want to store the integer value in a String, use String.valueOf:
String pin = String.valueOf(randomPIN);
Use a string to store the value:
String PINString= String.valueOf(randomPIN);
Try this approach. The x is just the first digit. It is from 1 to 9.
Then you append it to another number which has at most 3 digits.
public String generatePIN()
{
int x = (int)(Math.random() * 9);
x = x + 1;
String randomPIN = (x + "") + ( ((int)(Math.random()*1000)) + "" );
return randomPIN;
}
This question already has answers here:
Formatting a String to Remove Scientific Notation - Java
(4 answers)
Closed 9 years ago.
I'm trying to convert a HEX string into float from a data that comes from a device:
The device output in the LCD display,
0x00ac and the corresponding float value is 5.06
The method that calculated the value is:
final byte[] temp = new byte[1];
temp[0] = ba[0];
float fff = hexToFloat(bytesToHex(temp)).floatValue();
final float ff = ( fff / 42) * 1000;
String floatString = Float.toString(ff);
However the floatString output string contains "E-" notation. I need to remove this. Also it seems that the value of ff is slightly different from what the device output in the LCD.
I don't see how 0x00ac can be equal to 5.06, but here is how to get rid of the scientific notation with BigDecimals:
BigDecimal num = new BigDecimal(fltInput);
String numWithNoExponents = num.toPlainString()