I am trying to convert "0x6042607b1ba01d8dl" into a long.
I have tried:
long value = new BigInteger("0x6042607b1ba01d8dl", 16).longValue();
long value = new BigInteger("0x6042607b1ba01d8dl", 32).longValue();
long value = Long.decode("0x6042607b1ba01d8dl");
Long.parseLong("0x6042607b1ba01d8dl");
Note: The Hex number "0x6042607b1ba01d8dl" has 17 numbers
From the javadoc for the BigInteger(String,int) constructor:
The String representation consists of an optional minus or plus sign followed by a sequence of one or more digits in the specified radix.
So you just need to remove the 0x from your string:
long value = new BigInteger("6042607b1ba01d8d", 16).longValue();
The BigInteger constructor does not understand your 0x prefix.
Use e.g.
long value = new BigInteger("6042607b1ba01d8d", 16).longValue();
Or:
String number = "0x6042607b1ba01d8d";
long value = new BigInteger(number.subString(2), 16).longValue();
You can also use Long.decode(), which does accept a 0x prefix for decoding hex.
You can try this:
long value = Long.parseLong("6042607b1ba01d8d", 16);
Long.parseLong can sometimes fail for unsigned longs, so, the BigInteger approaches are better.
As said by the above answers in code form:
String bigHexNumber = "0x6042607b1ba01d8d";
if(bigHexNumber.subString(0, 1).equals("0x") {
bigHexNumber = bigHexNumber.subString(2);
}
long hexInLongForm = new BigInteger(bigHexNumber, 16).longValue();
Related
I am trying to convert a long number (convertex from Hex to Long) to a byte array. I'm trying the following code:
ByteBuffer b = ByteBuffer.allocate(4);
// The literal 4328719365 of type int is out of range
b.putLong(4328719365);
byte[] result = b.array();
but it's not compiling due to being out of range for int.
What can I do to solve this issue?
Suffix L (or l) converts literal number to a long.
So try this:
b.putLong(4328719365L);
You can use literal long value just like number without L suffix. Like assign them to variables:
long myLongValue = 4328719365L;
b.putLong(myLongValue);
1) Suffix L to the value
2) Increase the ByteBuffer allocated size and try
3) Int will take max of 10 digits and putLong might internally add L and making it beyond 10. Please check reducing digits in number.
I am working on a program where I am trying to take a binary string and convert it to an integer using this piece of code:
int x = Integer.parseInt(arrayList.get(count), 2);
//arrayList is a collection of 30 character binary strings
//count is an incrementing integer used to choose which string to use while inside of a while loop
I have tested this program with strings such as "001010", however with larger strings such as "100000110000010100001111010110" compiles, but terminal output gives me an error:
"# java.lang.NumberFormatException.forInputString(NumberFormatException.java:(line number))
How can I fix this?
You can try java.math.BigInteger.
String bin = "100000110000010100001111010110";
BigInteger bi = new BigInteger(bin, 2);
System.out.println(bi);
Output:
549536726
You can also do it using Long.valueOf(). However, with Long your binary strings can be up to 63 bits long, whereas with BigInteger the length can be of arbitrarily many bits.
String bin = "100000110000010100001111010110";
long biLong = Long.valueOf(bin, 2);
System.out.println(biLong);
Try Long.parseLong method instead of Integer.parseInt
i have a hex value "FF30" in string format.i need the two's complement value of this number.now i am doing,first converting it to binary then taking the 2's complement.is there is any simple way.
Do you need to apply the 2's complement operation to this value, or do you need to just interpret it as a 2's complement number? If the latter, then skip the second line of code below.
I think this will work for you.
int val = Integer.parseInt("FF30", 16);
int result = (~val) + 1;
Update
Since you imply in your comments that you want the result converted back to a string, that's a simple matter of calling "toHexString" and chopping off any padded bits that were prefixed
String hex = "FF30";
int length = hex.length();
long value = Long.parseLong(hex, 16); // convert hex string to long
long result = (~value) + 1; // compute the 2's complement
// convert the result value back to a hex string (keeping the same length and dropping any sign-extension bits)
String resultAsString = Long.toHexString(result);
// chop off the prefix of the string so the result is the same length as the input
int newLength = resultAsString.length();
if (newLength > length)
{
resultAsString = resultAsString.substring(newLength-length);
}
System.out.print(resultAsString);
I have to convert a String "1392298553937999872" into an int. The string is a timestamp. Normally this should be possible to convert it into a int by using:
Integer i = Integer.valueOf(1392298553937999872);
But I receive the following exception:
java.lang.NumberFormatException: For input string:
"1392298553937999872"
If I use double it works, but the number is wrong. So how can I convert a timestamp into an int?
convert the string to long.
String a="1392298553937999872";
long b= Long.parseLong(a);
The number is greater than the max value of Integer use Long
Long l = new Long("1392298553937999872");
The number you're trying to convert exceeds the Integer.MAX_VALUE value. You'd better use BigInteger.
BigInteger bigInteger = new BigInteger("1392298553937999872");
The value you're trying to convert is greater than the Integer.MAX_VALUE (2,147,483,647), you should use one of the alternative types Long, BigInteger:
Long bigValue = Long.parseLong("1392298553937999872");
// ...
Long bigValue = new Long("1392298553937999872");
// ..
Long bigValue = Long.valueOf("1392298553937999872");
// ...
BigInteger bigValue = new BigInteger("1392298553937999872");
If adding an additional library and the values might vary, you can also use apache commons' NumberUtils. The method NumberUtils.createNumber(String) will adapt based on the provided input:
Number bigValue = NumberUtils.createNumber(inputString);
I have a hex String 240300000800000000000000004.
I want to add integer in this hex code and get back hex code in java.
As java does not support this muck length of value, so how do we do this?
Thanks
Abhishek
String str = "240300000800000000000000004";
BigInteger bi = new BigInteger(str, 16);
Integer ii = 10; // Integer to add
bi = bi.add(new BigInteger(ii.toString()));
System.out.println(bi.toString(16)); // 24030000080000000000000000e
Use BigInteger in Java
That will satisfy your requirements.
Visit the following tutorial to learn BigInteger in Java : TutorialPoint
The easiest solution is the already posted one of using BigInteger. However, you can also process the addition in chunks such that the result and any carry will fit in a long. Start at the right hand end with carry-in zero. Take a group of up to 15 hex digits, convert to long, do the addition including any carry-in from the addition to the right of this chunk, store the 15 least significant digits of the result into the output string, and remember the most significant digit to use as the carry-out from this addition, the carry-in to the next addition to the left.
BigInteger b = new BigInteger("FF", 16);
BigInteger bPlus1 = b.plus(1);
String hex = bPlus1.toString(16); // 0x100
BigInteger bigint = new BigInteger("240300000800000000000000004" ,16);
System.out.println(bigint);
BigInteger s = new BigInteger("8");
bigint = bigint.add(s);
System.out.println(bigint);