I tried to convert Hex to BigInteger, here is source code:
public static void main (String[] args)
{
BigInteger a = new BigInteger("c6e87a3b3ef791287ac85a0c7836dd8305dd9a4c0024123db9145bb5067a8abf142e9001b0788039fd4d676b59db2110da23532282c7648d94fdbf29b731b0d21f9ca51acd44063f271326915283af97f0822519bbe2a6b80618e45e6194b2445d5afe70cf2c10569034966f3bc3b9a30d3ac4f06dbbca89fce7ef64ee14de3dL", 16);
BigInteger b = new BigInteger("9e5d1bd4f53eebc8a695c61ba4436e38af273fd6733115611fded8dd407b5f0bc04301829dc6ed921af866c3c7977839fc75831152307f8e50e3c0f9107b6ae82ddab584807ea5ba7f32f9bfcab6218c6c6367817dfdd3b2ccc5c21cc9550b9248cac34dfb0d22151c196ca843f15614b3f6b044f9c5e727dc0b44f441c2ed7fL", 16);
System.out.println(a);
System.out.println(b);
// System.out.println(modInv(a, b));
}
I tried to run this source code, but I got Runtime error like this:
Exception in thread "main" java.lang.NumberFormatException: For input
string: "....14de3dL" at
java.lang.NumberFormatException.forInputString(Unknown Source) at
java.lang.Integer.parseInt(Unknown Source) at
java.math.BigInteger.(Unknown Source) at
sss.main.main(main.java:15)
Is there something problem in source code? I can't find it.
As pointed out in comment L is not part of Hexadecimal presentation.L is used to represent the string as Long, so it is an invalid character in the hexadecimal presentation. Code below should be helpful
BigInteger a = new BigInteger("c6e87a3b3ef791287ac85a0c7836dd8305dd9a4c0024123db9145bb5067a8abf142e9001b0788039fd4d676b59db2110da23532282c7648d94fdbf29b731b0d21f9ca51acd44063f271326915283af97f0822519bbe2a6b80618e45e6194b2445d5afe70cf2c10569034966f3bc3b9a30d3ac4f06dbbca89fce7ef64ee14de3d", 16);
System.out.println(a.toString());
First of All, L is not included in Hex Numbers that you wrote in the end of both strings. you can visit this link for help on Hex Numbers
https://en.wikipedia.org/wiki/Hexadecimal
After visiting this , please view this post as well
Java convert a HEX String to a BigInt
Related
I'm trying to convert my binary string to decimal value but i'm getting error.
The binary string is of 32 bits so no case of overflow.
String s = "11111111111111111111111111111101";
System.out.print(s.length());
System.out.print(Integer.parseInt(s,2));
Exception is:-
Exception in thread "main" java.lang.NumberFormatException: For input string: "11111111111111111111111111111101"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:583)
Integer type is not big as you expect, instead, try to use BigInteger:
new BigInteger(s, 2)
Output
324294967293
The first bit is 1, so this number cannot be represented as a positive int. If you try, it will "overflow" and become negative.
If that is what you want, you can parse the string as an unsigned value:
System.out.print(Integer.parseUnsignedInt(s,2));
Output: -3
You can use long:
String s = "11111111111111111111111111111101";
System.out.print(s.length());
System.out.print(Long.parseLong(s,2));
Hello here i want to convert Byte array ie 0x3eb to short so i considered 0x3eb as a string and tried to convert to short but its throwing Numberformat Exception...someone please help me
import java.io.UnsupportedEncodingException;
public class mmmain
{
public static void main(String[] args) throws UnsupportedEncodingException
{
String ss="0x03eb";
Short value = Short.parseShort(ss);
System.out.println("value--->"+value);
}
}
Exception what im getting is
Exception in thread "main" java.lang.NumberFormatException:
For input string: "0x3eb" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:491)
at java.lang.Short.parseShort(Short.java:117)
at java.lang.Short.parseShort(Short.java:143)
at mmmain.main(mmmain.java:14)
even i tried converting 0x3eb to bytes by
byte[] bytes = ss.getBytes();
but i didnt found any implementation for parsing bytes to short.
Thanks in advance
See the doc of parseShort:
Parses the string argument as a signed decimal short. The characters
in the string must all be decimal digits, except that the first
character may be an ASCII minus sign '-' ('\u002D') to indicate a
negative value or an ASCII plus sign '+' ('\u002B') to indicate a
positive value.
The string to be parsed should only contain decimal characters and sign characters, it can not contains the 0x prefix.
Try:
String ss="3eb";
Short value = Short.parseShort(ss, 16);
Since the string value that you're using is a hexadecimal value, to convert it into short, you need to remove the 0x using a substring and pass the radix as below:
Short.parseShort(yourHexString.substring(2), 16)
Here 16 is the radix. More info in the doc here.
Update
Since the OP asked for some more clarification, adding the below info.
The short datatype can only have values between -32,768 and 32,767. It can't directly hold 0x3eb, but it can hold the equivalent decimal value of it. That's why when you parse it into the short variable and print, it shows 1003, which is the decimal equivalent of 0x3eb.
You have to cut "0x" from the beginning:
short.parseShort(yourHexString.Substring(2), 16)
Follow this document this may help you String to byte array, byte array to String in Java
I'm doing a project and i have a really simple hash function in java that SHOULD read each "data" (which is a generic type that is a String or Double type read by file) character and make a sum of their values that will be used as hashcode.
I thought that i could convert each character to Hexadecimal, and then "decode" or "parseInt" the obtained String, but it does not work and i do not understand why.
Here is my method:
public long HashFunction(T data){
String bytes = data.toString();
int value=0;
for (int i=0; i<bytes.length(); i++)
value = value + Integer.decode(Integer.toHexString( bytes.charAt(i) | 0x100000).substring(1));
return (value%1583)%(size);
//1583 prime number not near to the power of 2, size is the size of the array of my hashtable
}
And here is my error, 0038 should be an "8":
Exception in thread "main" java.lang.NumberFormatException: For input string: "0038"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.valueOf(Unknown Source)
at java.lang.Integer.decode(Unknown Source)
at dizionario_package.HashTable.HashFunction(HashTable.java:22)
at dizionario_package.HashTable.HashInsert(HashTable.java:29)
at dizionario_package.RecordReader.CreateHTFromFile(RecordReader.java:24)
at dizionario_package.proviamo.main(proviamo.java:8)
Also, i'm sure that the error is in this function, because if i use the java hashcode method, it works.
Thanks in advance.
You need to tell decode you're using hex. Prefix the string with 0x.
value = Integer.decode("0x"+"10038".substring(1));
The following code is for debugging:
public static void main(String[] args) {
BigInteger n = new BigInteger("10000000001");
String sn = n.toString();
char[] array = sn.toCharArray();
//intend to change value of some char in array
//not standard math operation
BigInteger result = new BigInteger(array.toString());
}
It gives me error:
Exception in thread "main" java.lang.NumberFormatException: For input string: "[C#86c347"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.math.BigInteger.<init>(BigInteger.java:316)
at java.math.BigInteger.<init>(BigInteger.java:451)
at debug.main(debug.java:14)
But it was working fine, until this case, I'm not quite sure what went wrong.
When in doubt, add more diagnostics... taking out statements which do two things. This:
array.toString()
won't do what you expect it to, because arrays don't override toString(). It'll be returning something like "[C#89ffb18". You can see this by extracting an extra local variable:
BigInteger n = new BigInteger("10000000001");
String sn = n.toString();
char[] array = sn.toCharArray();
String text = array.toString();
BigInteger result = new BigInteger(text);
Then in the debugger you should easily be able to look at the value of text before the call to BigInteger - which would show the problem quite clearly.
To convert a char array to a string containing those characters, you want:
new String(array)
I am getting an error because of the following line of code:
int x = color(Integer.parseInt("ffffffde",16));
I think it might be because it is a minus value
Any ideas why or how or how to fix it?
EDIT:
Sorry, didn't include the actual error. here it is:
Exception in
thread "Animation Thread" java.lang.NumberFormatException: For input
string: "ffffffde" at
java.lang.NumberFormatException.forInputString(Unknown Source) at
java.lang.Integer.parseInt(Unknown Source)
EDIT 2:
The value ("ffffffde") is being created by the following code:
Integer.toHexString(int_val);
EDIT 3:
Turns out it is a known bug (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4215269)
Although you can convert integers to hex strings, you cannot convert them back if they are negative numbers!!
ffffffde is bigger than integer max value
Java int is 32 bit signed type ranges from –2,147,483,648 to 2,147,483,647.
ffffffde = 4,294,967,262
Edit
You used Integer.toHexString(int_val) to turn a int into a hex string. From the doc of that method:
Returns a string representation of the integer argument as an unsigned integer in base 16.
But int is a signed type.
USE
int value = new BigInteger("ffffffde", 16).intValue();
to get it back as a negative value.
If you are getting error like this,
Exception in thread "main" java.lang.NumberFormatException: For input string: "ffffffde"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:461)
at com.TestStatic.main(TestStatic.java:22)
Then there is problem with value you are passing that is ffffffde . This is not a valid hex value for parsing to int.
Please try this
int x = Integer.parseInt("ffffde",16);
System.out.println(x);
It should work.
For hex values more than that you have to pars to Long
Long x = Long.parseLong("ffffffde",16);
System.out.println(x);
And this also should work