fork download
  1. /* package whatever; // don't place package name! */
  2. import java.math.BigDecimal;
  3. import java.math.BigInteger;
  4.  
  5. /* Name of the class has to be "Main" only if the class is public. */
  6. class Ideone
  7. {
  8. public static void main(String[] args) {
  9. Ideone numTest = new Ideone();
  10. numTest.numberTestDemo();
  11. }
  12.  
  13. private void numberTestDemo() {
  14.  
  15. /*Decimal String Part*/
  16. String decimalString = "998446744073709551615";
  17. System.out.println("\nDecimal String Part : ");
  18.  
  19. BigInteger bigInt4mDecimalString = decimal2Exponent(decimalString);
  20. System.out.println("\nDecimal-String Radix Info : ");
  21. showRadixInfo(bigInt4mDecimalString);
  22.  
  23. byte[] byte4mDec = bigInt4mDecimalString.toByteArray();
  24. System.out.println("\nDecimal-String Byte Info : ");
  25. printByteInfo(byte4mDec);
  26.  
  27. /*Exponent String Part*/
  28. /* Copying the same exact Scientific notation from decimalString to exponentString*/
  29. Double dbl = bigInt4mDecimalString.doubleValue();
  30. String exponentString = dbl.toString();
  31.  
  32.  
  33. System.out.println("\nExponent String Part : ");
  34. BigDecimal bigDecimal4mExponentString = exponent2Decimal(exponentString);
  35.  
  36. System.out.println("Exponent String Radix Info : ");
  37. showRadixInfo(bigDecimal4mExponentString.toBigIntegerExact());
  38.  
  39. byte[] byte4mExp = bigDecimal4mExponentString.unscaledValue().toByteArray();
  40. System.out.println("Exponent String Byte Info : ");
  41. printByteInfo(byte4mExp);
  42.  
  43. if (bigDecimal4mExponentString.toBigIntegerExact().equals(bigInt4mDecimalString))
  44. System.out.println("\n\nBoth BigInts are equal");
  45. else
  46. System.out.println("Both BigInts are not equal");
  47. }
  48.  
  49. private BigInteger decimal2Exponent(String decimalString) {
  50. BigInteger bigInt = new BigInteger(decimalString);
  51. System.out.println("decimalString : " + decimalString);
  52. System.out.printf("Scientific notation in double format : %g\n", bigInt.doubleValue());
  53.  
  54. Double dbl = bigInt.doubleValue();
  55. System.out.println("Scientific notation in String format : " + dbl.toString());
  56. return bigInt;
  57. }
  58.  
  59. private BigDecimal exponent2Decimal(String exponentString) {
  60. BigDecimal bigDecimal = new BigDecimal(exponentString);
  61. System.out.println("exponentString : " + exponentString);
  62. return bigDecimal;
  63. }
  64.  
  65. private void showRadixInfo(BigInteger bigInt) {
  66. System.out.println("Binary : " + bigInt.toString(2));
  67. System.out.println("Octal : " + bigInt.toString(8));
  68. System.out.println("Decimal : " + bigInt.toString(10));
  69. System.out.println("Hexa : 0x" + bigInt.toString(16).toUpperCase());
  70. System.out.println("Bit length : " + bigInt.bitLength());
  71. System.out.println("Bit count : " + bigInt.bitCount() + "\n");
  72. }
  73.  
  74.  
  75. private void printByteInfo(byte[] buff) {
  76.  
  77. System.out.print("\nByte Array : [");
  78. for(int i = 0; i < buff.length; i++)
  79. System.out.print((buff[i] & 0xFF) + ",");
  80. System.out.print("]");
  81.  
  82. System.out.println("\nBinary-Byte : ");
  83. for(int i = 0; i < buff.length; i++) {
  84. System.out.print(toBinaryString(buff[i]));
  85. System.out.print("\t");
  86. }
  87.  
  88. BigInteger bi = new BigInteger(buff);
  89. String hexByte = bi.toString(16);
  90. System.out.println("\nHex-Byte : 0x" + hexByte.toUpperCase());
  91. }
  92.  
  93. private static String toBinaryString(byte n) {
  94. StringBuilder sb = new StringBuilder("00000000");
  95. for (int bit = 0; bit < sb.length(); bit++) {
  96. if (((n >> bit) & 1) > 0)
  97. sb.setCharAt(7 - bit, '1');
  98. }
  99. return sb.toString();
  100. }
  101. }
Success #stdin #stdout 0.1s 28464KB
stdin
Standard input is empty
stdout
Decimal String Part  : 
decimalString  : 998446744073709551615
Scientific notation in double format : 9.98447e+20
Scientific notation in String format : 9.984467440737096E20

Decimal-String Radix Info : 
Binary : 1101100010000000111011011001111011110011001010110011111111111111111111
Octal : 154200733173631263777777
Decimal : 998446744073709551615
Hexa : 0x36203B67BCCACFFFFF
Bit length : 70
Bit count : 46


Decimal-String Byte Info : 

Byte Array : [54,32,59,103,188,202,207,255,255,]
Binary-Byte : 
00110110	00100000	00111011	01100111	10111100	11001010	11001111	11111111	11111111	
Hex-Byte : 0x36203B67BCCACFFFFF

Exponent String Part : 
exponentString  : 9.984467440737096E20
Exponent String Radix Info : 
Binary : 1101100010000000111011011001111011110011001010110100001011110100000000
Octal : 154200733173631264136400
Decimal : 998446744073709600000
Hexa : 0x36203B67BCCAD0BD00
Bit length : 70
Bit count : 33

Exponent String Byte Info : 

Byte Array : [35,120,209,251,19,155,72,]
Binary-Byte : 
00100011	01111000	11010001	11111011	00010011	10011011	01001000	
Hex-Byte : 0x2378D1FB139B48
Both BigInts are not equal