Using Java to realize the reciprocal mechanism between decimal and binary decimal

Source: Internet
Author: User
Tags decimal to binary
using Java to realize the reciprocal mechanism between decimal and binary decimal

This blog mainly describes the use of Java to achieve decimal and binary decimal number of the mutual transfer (do not invoke Java-brought functions). Later may use the MATLAB realization, because our information security one experiment is the request Matlab realizes. Required

An experimental requirement of information security course
1, write the program to achieve decimal decimal x=0.7 binary conversion, and analyze its error (that is, the binary decimal decimals into decimal decimals, and the original decimal decimal to compare.) )
2, the programming realizes the decimal number x=5.9 the binary transformation. train of Thought

The second problem of the idea is that the decimal is divided into integral parts and small parts to achieve. Implement

Matla implementation

%% Clear
CLC
n=0.7;
m=20;% retains 10 decimal places
, multiplies decimal times 2 with floor, and then loops through the remaining numbers
D=char (mod (n*2.^ (1:m), 2) + ' 0 ');%char function to create a character matrix
D1=[d (1:end-m), '. ', D (end-m+1:end)]% display binary conversion decimal
f=d-' 0 ';
F1=sum (f./(2.^ (1:m))   %% binary Conversion decimal
error = N-F1

Java language Implementation
Execution Environment: Jdk1.8,intellij idea 2017.3.4 x64.

Import static java.lang.System.out; /** * Realize binary and decimal interchange * @author Canlong * @time 2018/4/13/public class Test3 {/** * 1, write program to achieve decimal decimal x=0.7 binary and analyze its error (that is, to convert the binary decimal decimals to decimal decimals and compare them with the original decimal decimals).
     ) 2, the programming realizes the decimal number x=5.9 the binary transformation.
        * @param args */public static void main (string[] args) {//decimal decimal Double x = 0.7 to convert;
        Converts decimal decimals to binary String binxstr = Decxiao2bin (x);
        Converts a binary decimal number to decimal double decx = Bin2decxiao (BINXSTR);

        Out.println ("Error:" + (X-DECX));
        Converts a decimal number to binary double decX1 = 5.9;
        int decint = (int) math.floor (decX1);
        Double Decxiao = decx1-decint;
        String binint = Decint2bin (decint);
        String Binx = Decxiao2bin (Decxiao);
        Out.println ("5 corresponding binary system is:" +binint); Out.println ("5.9) corresponds to the binary system:" +binint+ "."
    +binx);
        Convert decimal integer to binary public static String decint2bin (int decint) {int index = 0;
        int rem = 1;
        String binstr= "";
   while (decint!=1) {         rem = decint%2;
            Decint = DECINT/2;
            Binstr + + integer.tostring (REM);
            if (Decint = = 1) {binstr+=integer.tostring (decint);
    } return BINSTR;  /** * Converts decimal decimals to binary/public static String Decxiao2bin (double x) {//exact digit int accurate
        = 100;
        int[] Binx = new Int[accurate];
        String binxsb= "";
        Double x1 = x;
        Double x2=0;
            for (int i=0;i<binx.length;i++) {x2 = x1+x1;
            X1 =math.floor (x2);
            binx[i]= (int) x1;
            x1=x2-x1;
        BINXSB + + integer.tostring (Binx[i]);
        String binxstr = binxsb.tostring ();
        The approximate binary number of out.println (x+) is (accurate to the decimal point "+accurate+" bit): "+binxstr";
    return binxstr; /** * Converts binary decimals to decimal * @param binxstr binary decimal * @return Decimal decimal */public static double Bin2decxi
        AO (String binxstr) {double decx = 0.0;
Number of digits        int k = 0;
            for (int i=0;i<binxstr.length (); i++) {int exp = Binxstr.charat (i)-' 0 ';
            Exp =-(i+1) *exp;
            if (exp!=0) {decx + = Math.pow (2, exp); } out.println ("Binary decimal is;" +binxstr+ ".
        \ r \ n its corresponding decimal is: "+DECX";
    return decx; }
}
Results

Error Analysis:
Tested many times, Java code and MATLAB to convert decimal decimal to binary decimal, if using double to store, can only be converted to binary to 53 digits after the decimal point, and then the number is 0. And because of the double value, Java can only be stored to 16 digits after the decimal point, as long as within this range, using the above conversion code, the resulting error is 0. As for why the use of array storage binary can only be stored in the binary number of 53 digits, the reason remains to be explored. Initially suspected that because decimal decimals converted to binary decimal, the whole part of the integer, resulting in a small number of decimal parts, even if several times doubled, still can not reach 1 or the computer store a decimal the largest binary decimal is the decimal point after 53 digits.
Later, after the output of each step of the transformation of decimal decimal, found that every time in the 50th step, the decimal decimal will become 0.5, so the reason should be as long as the decimal decimal point is 16 digits, can be doubled by 50 times (double before removing the integer part) into 0. Summary

In general, this implementation is not too difficult, just because of their unfamiliar with the way the operation, and caused themselves to write a lot of redundant code and waste a lot of time to complete this class experiment. Next time we have a chance to refine. As for the implementation of the principle, but also the next time when the opportunity to say. In fact, the Java interior has functions to achieve decimal and binary interchange, but after their own binary and decimal conversion from the principle level up to achieve, can exercise their thinking ability. After this practice, I found that the binary and decimal conversion theory is not too familiar, Java implementation is very inefficient. However, after this practice, I have deepened the understanding of the theory and practice part of this transformation, and will continue to study later.

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.