Conversion from decimal to binary in Java

Source: Internet
Author: User

There are many conversion methods between a decimal number and a binary number in different development languages. In Java, it is very easy to convert a decimal integer into a binary number, you only need to use integer. the tobinarystring (INT) method returns the result. However, if the conversion is not an integer but a decimal number with a decimal part, it is not that easy. Since integers and decimals are converted in different ways, first convert the integers and decimals of the decimal number, and then merge and convert the results to get the desired result.

The algorithm for converting a decimal number to a binary number is as follows:
1. convert a decimal integer to a binary integer
To convert a decimal integer to a binary integer, use the "Division 2 remainder, reverse order" method. The specific method is: remove the decimal integer with 2 to get a quotient and remainder; then remove the quotient with 2 to get a quotient and remainder, so that until the quotient is zero, then, the obtained remainder is used as the low-level valid bit of the binary number, and the obtained remainder is arranged in sequence as the high-level valid bit of the binary number.
2. Convert decimal to binary decimal
To convert decimal places into binary decimal places, use the "take 2 as an integer and arrange them in sequence" method. The specific method is: Use 2 to multiply decimal places to get the product, take out the integer part of the product, and then use 2 to multiply the remaining decimal part to get another product, then, the integer part of the product is taken out until the fractional part in the product is zero or reaches the required precision. Then, sort the retrieved integers in order. The first integer is used as the high valid bits of the binary decimal places, and the second integer is used as the low valid bits.

The following is a Java program written based on the algorithm for converting the decimal number to the binary number for your reference:

  1. Import java. Math. bigdecimal;
  2. Public class test {
  3. Public static void main (string [] ARGs ){
  4. Test T = new test ();
  5. Double D = 8;
  6. String S = T. decimal2binarystr (d );
  7. System. Out. println ("decimal number" + D + "convert to binary number:" + S );
  8. }
  9. /**
  10. * Convert decimal to binary
  11. * @ Param D: decimal number
  12. * @ Return refers to a string converted from a decimal number to a binary number.
  13. */
  14. Public String decimal2binarystr (double D ){
  15. String result = decimal2binarystr_inte (d );
  16. Result + = decimal2binarystr_deci (d );
  17. Return result;
  18. }
  19. /**
  20. * Convert the part of a decimal integer to the binary number.
  21. * @ Param D: decimal number
  22. * @ Return refers to a binary string converted from a decimal integer.
  23. */
  24. Public String decimal2binarystr_inte (double D ){
  25. // Return integer. tobinarystring (INT) D );
  26. /*
  27. * The preceding integer. tobinarystring (INT) is used to obtain the binary result of the integer part,
  28. * To display the decimal to binary algorithm, select the following program for conversion.
  29. */
  30. String result = "";
  31. Long inte = (long) D;
  32. Int Index = 0;
  33. While (true ){
  34. Result + = inte % 2;
  35. Inte = inte/2;
  36. Index ++;
  37. If (index % 4 = 0 ){
  38. Result + = "";
  39. }
  40. If (inte = 0 ){
  41. While (index % 4! = 0 ){
  42. Result + = "0 ";
  43. Index ++;
  44. }
  45. Break;
  46. }
  47. }
  48. Char [] C = result. tochararray ();
  49. Char [] cc = new char [C. Length];
  50. For (INT I = C. length; I> 0; I --){
  51. CC [CC. Length-I] = C [I-1];
  52. }
  53. Return new string (CC );
  54. }
  55. /**
  56. * Decimal part to binary
  57. * @ Param D: decimal number
  58. * @ Return refers to a string that converts a decimal part into a binary decimal part.
  59. */
  60. Public String decimal2binarystr_deci (double D ){
  61. Return decimal2binarystr_deci (D, 0 );
  62. }
  63. /**
  64. * Decimal part to binary
  65. * @ Param D: decimal number
  66. * @ Param scale exact digits in the fractional part
  67. * @ Return refers to a string that converts a decimal part into a binary decimal part.
  68. */
  69. Public String decimal2binarystr_deci (double D, int scale ){
  70. Double deci = sub (D, (long) D );
  71. If (deci = 0 ){
  72. Return "";
  73. }
  74. // To prevent the program from returning an infinite loop binary decimal point to the converted data, a default precision is provided.
  75. If (scale = 0 ){
  76. Scale = (string. valueof (deci). Length ()-2) * 4;
  77. }
  78. Int Index = 0;
  79. Stringbuilder intestr = new stringbuilder ();
  80. Double tempd = 0.d;
  81. While (true ){
  82. If (deci = 0 | Index = scale ){
  83. While (index % 4! = 0 ){
  84. Intestr. append ("0 ");
  85. Index ++;
  86. }
  87. Break;
  88. }
  89. If (Index = 0 ){
  90. Intestr. append (".");
  91. }
  92. Tempd = deci * 2;
  93. Intestr. append (INT) tempd );
  94. Deci = sub (tempd, (INT) tempd );
  95. Index ++;
  96. If (index % 4 = 0 ){
  97. Intestr. append ("");
  98. }
  99. }
  100. Return intestr. tostring ();
  101. }
  102. /**
  103. * Provides precise subtraction operations.
  104. * @ Param V1 subtrahend
  105. * @ Param V2 subtrahend
  106. * @ Return Difference Between Two Parameters
  107. */
  108. Public static double sub (double V1, double V2 ){
  109. Bigdecimal b1 = new bigdecimal (double. tostring (V1 ));
  110. Bigdecimal b2 = new bigdecimal (double. tostring (V2 ));
  111. Return b1.subtract (B2). doublevalue ();
  112. }
  113. }

For example, convert the decimal number 1234.5 to the binary number 0100 1101

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.