0
[InputParam1:Decimal Number in String format(for Eg:30),
 InputParam2:Integer to denote number of repeating 0's to append(for Eg:6)]

For converting a number from Decimal to Binary and pad digits to its front I need to perform the following steps:

Step1: BigInteger binary=new BigInteger(InputParam1,2); -->This works when I define a BigInt with a base 16 (which I just tried when base2 failed) but not with a base 2 like above. It throws a numberformat exception.

Step 2: String pad=StringUtils.repeat("0",InputParam2); (to repeat 0 'InputParam2' number of times) -->This works fine

Step 3: Need to append pad in front of binary(from Step1) For a BigInteger, I'm not able to get something similar to .append. So I'm trying to a)convert this BigInteger number to String and b)append pad c)convert back to BigInteger (this step I'm not able to get without creating a new BigInteger)

Any pointers on Step1 and Step3-c would be helpful please.

trincot
  • 211,288
  • 25
  • 175
  • 211
SidTechs1
  • 67
  • 1
  • 12
  • Oops ! Its Java – SidTechs1 Mar 08 '19 at 21:54
  • 2
    Huh? It sounds like you're saying you want a BigInteger with 0's at the front of it. Is that what you're asking? That makes no sense. It's a number, pure and simple. Padding the front of a number is something you often do for display purposes, but that's all. So either you create a string so you can have '0' CHARS padding the front of the number-representing string, or you print a BigInteger, adding the zeros as part of printing it. – CryptoFool Mar 08 '19 at 22:01
  • Purpose is to create a converted number where a BigInt is padded with 0's in front for randomization purpose and return to the calling method. So, do you suggest having a String, where 0's are appended to the BigInt and returning that String itself? Also , any pointers on Step 1 pls ? – SidTechs1 Mar 08 '19 at 22:29
  • Yes. There's simply no concept of returning a number with padded zeros at tthe front of it. 00013 is the same number as 13, and a number is a number. – CryptoFool Mar 10 '19 at 00:06

1 Answers1

1

#1 - You haven't said what value you're passing in InputParam1, but my guess is that you're passing in something other than a string containing only '0' in '1' characters. The 'radix' parameter to the constructor tells the code how to interpret the string you're giving it. This works fine for me:

BigInteger binary=new BigInteger("1000",2);
System.out.println(binary);

How you construct the number has nothing to do with how you're eventually going to want to represent or display that number. The key idea here is that the representation of a number (hex, decimal, binary) has nothing to do with the number itself, just like leading zeros don't affect the value of number. 1111 binary, f hex, and 15 decimal are all the same number. If you pass the three of these into BigDecimal's constructor with a radix of 2, 16 and 10 respectively, you'll end up with EXACTLY the same thing in each case. The object will lose any notion of what kind of representation you used to set it to its initial value.

#3 - There is no concept of a number (int, BigInteger, etc.) with zero padding at the front, just as there's no notion of which base/radix you might use to represent a number visually. You can think about it conceptually, but that's just a way of displaying the number. It has nothing to do with the number itself.

I hadn't tried it before, but it seems there's no super simple way to format a binary value in Java with leading 0s, like there is for decimal and hex, since String.format() doesn't give a format specifier for binary. Per this StackOverflow post How to get 0-padded binary representation of an integer in java?, this seems to be about the best way to go, having converted the most accepted answer to work with BigInteger:

String str = String.format("%16s", binary.toString(2)).replace(' ', '0');

So here's all of my sample code, with output:

BigInteger binary=new BigInteger("1000",2);
System.out.println(binary);
String str = String.format("%16s", binary.toString(2)).replace(' ', '0');
System.out.println(str);

Output:

8
0000000000001000
CryptoFool
  • 15,463
  • 4
  • 16
  • 36