cottyengland
asked on
Java Long variabel type conversion
How can I convert the string '12345678901' into type long.
or
how can I get testx to equal 12345678901L (long)
StringBuffer serial_b = new StringBuffer( Long.toBinaryString(testx) );
Thak You
or
how can I get testx to equal 12345678901L (long)
StringBuffer serial_b = new StringBuffer( Long.toBinaryString(testx)
Thak You
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
long testx = Long.parseLong("1234567890 1");
you need to handle NumberFormatException
you need to handle NumberFormatException
Obviously if the integer is bigger than is able to fit in a long, you will have to use BigInteger:
BigInteger veryBigNumber = new BigInteger("12345678901123 4567890112 345678901" );
String binaryString = veryBigNumber.toString(2);
BigInteger veryBigNumber = new BigInteger("12345678901123
String binaryString = veryBigNumber.toString(2);
Actually, you can use the above code for any size integer.
Hi,
public class s2l {
public static void main (String[] args) {
// String s = "fred"; // do this if you want an exception
String s = "12345678901";
try {
long l = Long.parseLong(s.trim());
System.out.println("long l = " + l);
} catch (NumberFormatException nfe) {
System.out.println("Number FormatExce ption: " + nfe.getMessage());
}
}
}
R.K
public class s2l {
public static void main (String[] args) {
// String s = "fred"; // do this if you want an exception
String s = "12345678901";
try {
long l = Long.parseLong(s.trim());
System.out.println("long l = " + l);
} catch (NumberFormatException nfe) {
System.out.println("Number
}
}
}
R.K
Try:
long longPrimative = Long.parseLong("1234567890
Joe P