Rubbal Bhusri wrote:Can anyone please tell me what is going on in this code snippet :
BigInteger bi = new BigInteger("1023");
// Parse and format to binary
bi = new BigInteger("1111111111", 2); // 1023
String s = bi.toString(2); // 1111111111
The thing which i didn't get is : the purpose of arguments passed in BigInteger("1111111111", 2) constructor.
I mean what are we doing in this code ???
Is it meant to convert Binary numbers into String format ???
and Do we have to supply a Binary number everytime ???
what-if I want a method that convert a Binary number asked by a User from Command-line to Decimal format ??
public BigInteger(String val,
int radix)
Translates the String representation of a BigInteger in the specified radix into a BigInteger. The String representation consists of an optional minus sign followed by a sequence of one or more digits in the specified radix. The character-to-digit mapping is provided by Character.digit. The String may not contain any extraneous characters (whitespace, for example).
Parameters:
val - String representation of BigInteger.
radix - radix to be used in interpreting val.
public String toString(int radix)
Returns the String representation of this BigInteger in the given radix.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:How a number is written or displayed is independent of what the value is. "five" is the same as "5" is the same as "V" is the same as "0101"
Now, you have to tell someone what schema you are using. I would have to tell you the above are "english words", "decimal digit", "roman numerals" and "binary string".
BigInteger works the same way. you can pass in values, but you have to tell java whether it is binary, hexidecimal, or something else.
Rubbal Bhusri wrote:Can you please , show me some working,practical, live example of this method ???
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Rubbal Bhusri wrote:Seeing this , I thought that this BigInteger(String val, int radix) constructor is useless.
Can you please , show me some working,practical, live example of this method ???
The integer is :127
1111111
127
7f
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rubbal Bhusri wrote:I got this code working, where I used the method intValue() of class BigInteger to convert the String passed as argument to the BigInteger("0000000011", 2) constructor to an int type. And it worked very well.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
expectation is the root of all heartache - shakespeare. tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|