Convert.ToInt64 equivalent in java - java

I try the following code in c# and it give me the result as follow:
long dec1 = Convert.ToInt64("B62FD56EFD5B375D", 16);
result : -531879796222753398
I am trying to do this in java, but I always get NumberFormatException, because there are alphanumeric inside the String. What I code in java is:
Long.parseLong("B62FD56EFD5B375D", 16);
May I know what is the equivalent of this in java?

You can use Long.parseUnsignedLong in Java to get the same result.
long result = Long.parseUnsignedLong("B62FD56EFD5B375D", 16);

Maximum value is 9,223,372,036,854,775,807 (inclusive) for a long value. When the value B62FD56EFD5B375D parsed it is 13,127,946,111,482,018,682 which is unable to hold in a long value.
So instead use BigInteger.

long dec1 = new BigInteger("B62FD56EFD5B375D", 16).longValue();

you can try with BigInteger
BigInteger value = new BigInteger(hex, 16);

Related

Convert String UUID to BigInteger in Java

I am trying to convert UUID which is coming as string to Big Integer but it's failing every time with Number Format exception as it need String Decimal as parameter. Is there any way we can achieve this.
String x = "6CFAFD0DA976088FE05400144FFB4B37";
I tried with radix also but output is different.
BigInteger big = new BigInteger(x, 0);
System.out.println(big);
Any help is appreciated, TIA.
You are supposed to be using radix 16 as your string has alphanumeric values from 0-9 and A-F, set value 16 in radix as you have hexadecimal string.
String x = "6CFAFD0DA976088FE05400144FFB4B37";
BigInteger big = new BigInteger(x, 16);
System.out.println(big);
OUTPUT
144859830291446118078300087367740640055
You need to set radix value to 16.
For hexadecimal String you need to define the radix value as 16

Java Big number negative?

I tried to get bignumber from a string, but the value in binary is wrong.
priKeyData = HexByteKit.Hex2Byte("b8dfc598d14c0bb032c1f4eb1fcdb033289002f38cc16b2120dfa697f8982bef");
BigInteger priKeyBN2 = new BigInteger(priKeyData);
String s3 = priKeyBN2.toString(2);
it gives:
-100011100100000001110100110011100101110101100111111010001001111110011010011111000001011000101001110000000110010010011111100110011010111011011111111110100001100011100110011111010010100110111101101111100100000010110010110100000000111011001111101010000010001
But the right one should be:
1011100011011111110001011001100011010001010011000000101110110000001100101100000111110100111010110001111111001101101100000011001100101000100100000000001011110011100011001100000101101011001000010010000011011111101001101001011111111000100110000010101111101111
http://www.mobilefish.com/services/big_number/big_number.php
The right one is 256 bit, so it overflows in Java bignumber class?
Then how can I use this 256 bit number for some steps in Java encryption algorithm?
Thanks.
I don't know what HexByteKit is, but constructing BigInteger from the hex string gives the right result:
BigInteger priKeyBN2 = new BigInteger("b8dfc598d14c0bb032c1f4eb1fcdb033289002f38cc16b2120dfa697f8982bef", 16);
String s3 = priKeyBN2.toString(2);

How do you convert a binary string like "10010010101" to a BigInteger decimal representation?

I know that integers have Integer.parseInt(string, 2); but when the value of the decimal is very large, how can we use BigInteger in this case?
BigInteger(giantBinaryString, 2);
There's a constructor for that.
Use the constructor BigInteger(String s)
documentation: http://docs.oracle.com/javase/6/docs/api/java/math/BigInteger.html
For a binary integer represented as a string, use
BigInteger(binaryString, 2);
In this case you can construct a BigInteger from a string which
denotes a very very large integer. This is the purpose of BigInteger.
If you don't use BigInteger it will overflow your int variable.
You can do this by calling.
BigInteger b = new BigInteger(str, radix);
where you pass in radix = 2 or radix = 10 or whatever
you need (seems you need 2 in your case) and str is the value.
Then you can use
b.toString(radix);
to output your BigInteger value in whatever radix you want.
I think you are looking for this BigInteger(binaryString, 2);

Datatype for the number

How should I change the geometry datatype to String in Java?
Use BigInteger.
BigInteger bi = new BigInteger("01060000000100000...", 16);
You could use BigInteger. It has a constructor taking a String and a radix as an argument.
So:
new BigInteger(theString, 16);

How to convert BigInteger to String in java

I converted a String to BigInteger as follows:
Scanner sc=new Scanner(System.in);
System.out.println("enter the message");
String msg=sc.next();
byte[] bytemsg=msg.getBytes();
BigInteger m=new BigInteger(bytemsg);
Now I want my string back. I'm using m.toString() but that's giving me the desired result.
Why? Where is the bug and what can I do about it?
You want to use BigInteger.toByteArray()
String msg = "Hello there!";
BigInteger bi = new BigInteger(msg.getBytes());
System.out.println(new String(bi.toByteArray())); // prints "Hello there!"
The way I understand it is that you're doing the following transformations:
String -----------------> byte[] ------------------> BigInteger
String.getBytes() BigInteger(byte[])
And you want the reverse:
BigInteger ------------------------> byte[] ------------------> String
BigInteger.toByteArray() String(byte[])
Note that you probably want to use overloads of String.getBytes() and String(byte[]) that specifies an explicit encoding, otherwise you may run into encoding issues.
Use m.toString() or String.valueOf(m). String.valueOf uses toString() but is null safe.
Why don't you use the BigInteger(String) constructor ? That way, round-tripping via toString() should work fine.
(note also that your conversion to bytes doesn't explicitly specify a character-encoding and is platform-dependent - that could be source of grief further down the line)
You can also use Java's implicit conversion:
BigInteger m = new BigInteger(bytemsg);
String mStr = "" + m; // mStr now contains string representation of m.
When constructing a BigInteger with a string, the string must be formatted as a decimal number. You cannot use letters, unless you specify a radix in the second argument, you can specify up to 36 in the radix. 36 will give you alphanumeric characters only [0-9,a-z], so if you use this, you will have no formatting. You can create: new BigInteger("ihavenospaces", 36)
Then to convert back, use a .toString(36)
BUT TO KEEP FORMATTING:
Use the byte[] method that a couple people mentioned. That will pack the data with formatting into the smallest size, and allow you to keep track of number of bytes easily
That should be perfect for an RSA public key crypto system example program, assuming you keep the number of bytes in the message smaller than the number of bytes of PQ
(I realize this thread is old)
To reverse
byte[] bytemsg=msg.getBytes();
you can use
String text = new String(bytemsg);
using a BigInteger just complicates things, in fact it not clear why you want a byte[]. What are planing to do with the BigInteger or byte[]? What is the point?
String input = "0101";
BigInteger x = new BigInteger ( input , 2 );
String output = x.toString(2);
//How to solve BigDecimal & BigInteger and return a String.
BigDecimal x = new BigDecimal( a );
BigDecimal y = new BigDecimal( b );
BigDecimal result = BigDecimal.ZERO;
BigDecimal result = x.add(y);
return String.valueOf(result);
https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html.
Every object has a toString() method in Java.

Categories