I'm looking for a way to convert a BigInteger into a very short String (shortest possible). The conversion needs to be reversible. The security of the conversion is not a big deal in this case. Would anyone have recommendations or samples of how they would go about solving this problem?
You can use a Base64 encoding. Note that this example uses Apache commons-codec:
BigInteger number = new BigInteger("4143222334431546643677890898767548679452");
System.out.println(number);
String encoded = new String(Base64.encodeBase64(number.toByteArray()));
System.out.println(encoded);
BigInteger decoded = new BigInteger(Base64.decodeBase64(encoded));
System.out.println(decoded);
prints:
4143222334431546643677890898767548679452
DC0DmJRYaAn2AVdEZMvmhRw=
4143222334431546643677890898767548679452
One easy way is to use BigInteger.toString(Character.MAX_RADIX). To reverse, use the following constructor: BigInteger(String val, int radix).
Related
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);
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);
I am making an application that involves a seed to generate a world and some games let you provide that seed with text. I'm wondering how would you 'convert' a string to an integer.
A simple way would be to use the ASCII values of all the characters and append them to a string which you would then parse to an integer, but that severely limits the size of the string. How would you be able to do this with a larger string?
EDIT: 64 bit not 32
I would just call String.hashcode(). The standard String.hashcode() function makes use of all characters in the target string and gives good dispersal.
The only thing I would question is whether 32 bits of seed is going to be enough. It might mean that your world generator could generate at most 232 different worlds.
Random seeds for Random can be at least 48-bit, ideally 64-bit. You can write your own hash code like this.
public static long hashFor(String s) {
long h = 0;
for(int i = 0; i < s.length(); i++)
h = h * 10191 + s.charAt(i);
return h;
}
The Standard way for converting a String to Integer is using Integer.parseInt(String);
You pass the string into this and it would convert the String to int. Try it and let me know!
Please have a look at the following machine code
0111001101110100011100100110010101110011011100110110010101100100
This means something. I need to convert this to string. When I use Integer.parseInt() with the above as the string and 2 as the radix(to convert it to bytes), it gives number format exception.
And I believe I have to seperate this into sets of 8 pieces (like 01110011 , 10111010, etc). Am I correct?
Please help me to convert this correctly to string.
Thanks
final String s =
"0111001101110100011100100110010101110011011100110110010101100100";
final StringBuilder b = new StringBuilder();
for (int i = 0; i < s.length(); i+=8)
b.append((char)Integer.parseInt(s.substring(i,i+8),2));
System.out.println(b);
prints "stressed"
A shorter way of reading large integers is to use BigInteger
final String s = "0111001101110100011100100110010101110011011100110110010101100100";
System.out.println(new String(new BigInteger('0'+s, 2).toByteArray(), 0));
prints
stressed
It depends on the encoding of the String.
An ASCII coded string uses 1 byte for each character while a unicode coded string takes 2 bytes for each character. There are many other types of encodings. The binary layout differs for each encoding.
So you need to find the encoding that was used to write this string to binary format
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.