This question already has answers here:
How to convert BigInteger to String in java
(9 answers)
Closed 6 years ago.
I have code which takes a BigInteger
48451322989516520225703217657832557994348537500303367742400825550923100192302069868489479191146175399344044876949227990959739850227034985347595351425385263774028421913031512265649684935654507691239234667482091135118571200215310568615906290473167269182601320011893758047720172195848415075205065039282385885704
And after I perform bigint.toString(16) I get this string:
44ff39b391fe68e6522d4e2fd99a6c5c77afdae691357f04e5e504790460e7a8e30b3d988e2c1ad316660af7d4e70c012ab711bb77a238f7c2281903523446677f3f26b5d7338c77939f9d97268125adf309aba85e9113f895e9d5179987ab02f3cc255c83e05579664cb08f79390373cb7cce5d280c6647091721567e029a08
which contains letters in it, so after I try and convert it back to a BigInteger I can't b/c it's telling me it's not a number b/c of the letters in the value.
How do you properly go from a BigInteger, to string, and then back to BigInteger? Here is what my code is:
BigInteger decryptedBI = resultBI.modPow(keyD, keyN); // my biginteger
String decrypted = decryptedBI.toString(16); // converted it to a string value
System.out.println(decryptedBI);
System.out.println(decrypted); // this is the decrypted hash
BigInteger has a constructor that takes in a String for the value and an int denoting the radix (in your case, 16).
See the API docs, but here's you how use it:
BigInteger newBI = new BigInteger(myString, 16);
Related
This question already has answers here:
Convert an integer to an array of digits
(24 answers)
Closed 2 years ago.
I would just like to ask how to convert an int to int array - for example:
int number = 12345;
to:
[1,2,3,4,5];
Btw - could not really find anything out there, so we may hope that someone know.
thanks in advance
You can do it like this:
int number = 12345;
int[] digits = String.valueOf(number).chars().map(c -> c-'0').toArray();
Explanation:
First convert int to string with String.valueOf(number) to be able to use the method chars() which get a stream of int that represents the ASCII value of each char in the string. Then, we use the map function map(c -> c-'0') to convert the ASCII value of each character to its value, subtracting the value of the ASCII code from the character '0' from the ASCII code of the actual character. Finally, the stream is converted to an array with toArray().
I am attempting to convert some BigInteger objects and math from Java to C#.
The Java flow is as follows:
1. Construct 2 BigIntegers from a base-10 string (i.e. 0-9 values).
2. Construct a third BigInteger from an inputted byte array.
3. Create a fourth BigInteger as third.modPow(first, second).
4. Return the byte result of fourth.
The main complications in converting to C# seem to consist of endianness and signed/unsigned values.
I have tried a couple different ways to convert the initial 2 BigIntegers from Java->C#. I believe that using the base-10 string with BigInteger.Parse will work as intended, but I am not completely sure.
Another complication comes from the use of a BinaryReader/BinaryWriter implementation, in C#, that is already big-endian (like Java). I use the BR/BW to supply the byte array to create the third BigInteger and consume the byte array produced from the modPow (the fourth BigInteger).
I have tried reversing the byte arrays for input and output in every way, and still do not get the expected output.
Java:
public static byte[] doMath(byte[] input)
{
BigInteger exponent = new BigInteger("BASE-10-STRING");
BigInteger mod = new BigInteger("BASE-10-STRING");
BigInteger bigInput = new BigInteger(input);
return bigInput.modPow(exponent, mod).toByteArray();
}
C#:
public static byte[] CSharpDoMath(byte[] input)
{
BigInteger exponent = BigInteger.Parse("BASE-10-STRING");
BigInteger mod = BigInteger.Parse("BASE-10-STRING");
// big->little endian
byte[] reversedBytes = input.Reverse().ToArray();
BigInteger bigInput = new BigInteger(reversedBytes);
BigInteger output = BigInteger.ModPow(bigInput, exponent, mod);
// little->big endian
byte[] bigOutput = output.ToByteArray().Reverse().ToArray();
return bigOutput;
}
I need the same output from both.
This question already has answers here:
How does Java handle integer underflows and overflows and how would you check for it?
(12 answers)
BigInteger.pow(BigInteger)?
(9 answers)
Closed 3 years ago.
I'm trying to do a BigInteger exponent calculation. For my calculation, I am trying to subtract m1 from m2 (both are BigInteger) and get a positive value, but if I convert this positive number to an int number, I end up getting a negative value.
BigInteger m1 = new BigInteger("2905012217");
BigInteger m2 = new BigInteger("534500746");
int exp = m2.subtract(m1).abs().intValue();
System.out.println(exp);
BigInteger g1 = new BigInteger("2");
BigInteger output_test_g1 = g1.pow(exp);
output:
m1:2905012217
m2:534500746
exp:-1924455825
Error:
Exception in thread "main" java.lang.ArithmeticException: Negative exponent
at java.base/java.math.BigInteger.pow(BigInteger.java:2401)
at FHEv1.homomorphicEqualityTest(FHEv1.java:195)
at FHEv1.main(FHEv1.java:323)
Integer overflow. How does Java handle integer underflows and overflows and how would you check for it?
Too high and overflow to Integer.MIN_VALUE and continue counting if nessecary. Too low and go to Integer.MAX_VALUE and continue counting if nessecary.
For example,
int A = Integer.MAX_VALUE;
A++;
//A should be Integer.MIN_VALUE, correct me if I am wrong
This question already has answers here:
What would be the fastest method to test for primality in Java?
(15 answers)
Closed 8 years ago.
I have created a method to create a 128 bit UUID string, I now want to check if this is a prime number or not. I cant put the string into an int because it is too big. Can anyone suggest how I would go about checking?
This is the code I have used for creating the UUID
public static String uuid()
{
UUID uuid = UUID.randomUUID();
long hi = uuid.getMostSignificantBits();
long lo = uuid.getLeastSignificantBits();
byte[] bytes = ByteBuffer.allocate(16).putLong(hi).putLong(lo).array();
BigInteger big = new BigInteger(bytes);
String numericUuid = big.toString().replace('-','1'); // just in case
//System.out.println(numericUuid);
return(numericUuid);
}
You could use BigInteger's isProbablePrime:
http://www.tutorialspoint.com/java/math/biginteger_isprobableprime.htm
If you pass a high certainty parameter (e.g. 100) then if this returns true, the probability of it actually being a prime number is extremely close to 1.
This question already has answers here:
How do I convert a String to an int in Java?
(47 answers)
Closed 5 years ago.
I have written a function to convert string to integer
if ( data != null )
{
int theValue = Integer.parseInt( data.trim(), 16 );
return theValue;
}
else
return null;
I have a string which is 6042076399 and it gave me errors:
Exception in thread "main" java.lang.NumberFormatException: For input string: "6042076399"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:461)
Is this not the correct way to convert string to integer?
Here's the way I prefer to do it:
Edit (08/04/2015):
As noted in the comment below, this is actually better done like this:
String numStr = "123";
int num = Integer.parseInt(numStr);
An Integer can't hold that value. 6042076399 (413424640921 in decimal) is greater than 2147483647, the maximum an integer can hold.
Try using Long.parseLong.
That's the correct method, but your value is larger than the maximum size of an int.
The maximum size an int can hold is 231 - 1, or 2,147,483,647. Your value is 6,042,076,399. You should look at storing it as a long if you want a primitive type. The maximum value of a long is significantly larger - 263 - 1. Another option might be BigInteger.
That string is greater than Integer.MAX_VALUE. You can't parse something that is out of range of integers. (they go up to 2^31-1, I believe).
In addition to what the others answered, if you have a string of more than 8 hexadecimal digits (but up to 16 hexadecimal digits), you could convert it to a long using Long.parseLong() instead of to an int using Integer.parseInt().