JAVA 16 binary (including decimal) and binary interchange __java

Source: Internet
Author: User

First of all, the whole idea:
Hex that contains decimals, we can first divide it into integers and decimal parts,
Then the string comparison is made and the corresponding substitution is made.

Contains the binary decimal, the idea is similar, need to notice that the left integer is not enough four digits of the 0, the right is the same.

Import Java.math.BigDecimal; public class Test13 {public static void main (string[] args) {//16-two//TODO automatically generated method stub String e = ' 0
        .77 ";
        String num1 = "";
        String num2 = ""; if (E.indexof (".")
            ==-1)//here to determine whether the decimal {num1 = e;
        num2 = "0"; else {string[] S1 = e.split ("\.");
            /Divide the decimal into two parts NUM1 = s1[0];
        num2 = s1[1];
        } System.out.println ("Integer is:" +num1);

        System.out.println ("decimal is:" +num2);
        String a1= "";
        String a3= "";
        String A2 = "";//the last output int i = 0, i1 = 0;
        int J1 = 0; string[] ej={"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101",
        "1110", "1111"};


        string[] bj={"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
        Process integer if (num1.equals ("0")) {a1 = "0"; } else{J1 = Num1.length ();
            I1 = 0;
                    for (i = 0; i<j1;i++) {for (int j = 0; j<ej.length;j++) {


                    SYSTEM.OUT.PRINTLN ("Intercepted Value:" +num1.substring (I1, i1+1));
                        if (num1.substring (I1, i1+1). Equals (Bj[j)) {System.out.println ("value of J" +j);
                        A1 =a1+ Ej[j];
                        I1 = I1 +1;

                    Break
        }} System.out.println ("Integer hexadecimal binary is:" +A1);
            ///Decimal parts if (Num2.equals ("0")) {a2 = A1;
            else {J1 = Num2.length ();//Calculate the number of cycles System.out.println (J1);
            I1 = 0;
                for (i = 0; i<j1;i++) {//ej2[i]=num1.substring (I, i+2);
          for (int j = 0; j<ej.length;j++) {          SYSTEM.OUT.PRINTLN ("Intercepted Value:" +num2.substring (I1, i1+1));
                        if (num2.substring (I1, i1+1). Equals (Bj[j)) {System.out.println ("value of J" +j);
                        A3 =a3+ Ej[j];

                        I1 = I1 +1;

                    Break
            }} System.out.println ("Decimal hexadecimal binary is:" +A3); A2 = a1+ "."
            +A3;



    } System.out.println ("Hexadecimal binary is:" +A2); }//above is the majority of the code, octal binary binary system is the same reason

Next we look at binary 16:

String s = "100001.01" Double s = double.valueof (e);
                        String num1 =string.valueof ((int) s);//integer int zj1 = (int) s;
                        BigDecimal B1 = new BigDecimal (e);
                        BigDecimal b2 = new BigDecimal (ZJ1);
                        Double zj2 = b1.subtract (b2). Doublevalue ();//Decimal part String NUM0 = string.valueof (ZJ2);
                        If there is no decimal point, direct use of split will be an error.

                        string[] S1 = num0.split ("\;");
                        String num2 = s1[1];//takes a decimal System.out.println directly ("Integer:" +num1);

                        System.out.println ("decimal is:" +num2);
                        String a1= "";
                        String a3= "";
                        String A2 = "";//the last output int i = 0, i1 = 0;
                        int J1 = 0; string[] ej={"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001 "," 1010 "," 1011 "," 1100 "," 1101 "," 1110 "," 1111 "};

                        string[] bj={"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
                        if (Num1.equals ("0")) {a1 = "0"; else {//process integer if (num1
                            . Length ()%4)!=0) {J1 = 4-num1.length ()%4;

                            for (i = 0;i<j1;i++) {num1 = "0" +NUM1;

                        } System.out.println ("0" +num1)//To the left of the calculation requires a few 0}
                        J1 = Num1.length ()/4;
                        System.out.println (J1);
      for (i = 0; i<j1;i++) {//ej2[i]=num1.substring (I, i+2);                      for (int j = 0; j<ej.length;j++) {Sys

                                TEM.OUT.PRINTLN ("Intercepted Value:" +num1.substring (I1, i1+4)); if (num1.substring (I1, i1+4). Equals (Ej[j)) {System.ou
                                    T.println ("The value of J" +j);
                                    A1 =a1+ Bj[j];
                                    I1 = I1 +4;

                                Break 
                        }} System.out.println ("Integer binary hexadecimal is:" +a1);
                            } if (Num2.equals ("0")) {
                        a2 = A1; else {//Decimal division if (NUM2.L Ength ()%4)!=0) {J1= 4-num2.length ()%4;//need to add several 0 for (i = 0;i<j1;i++) { num2 = num2+ "0";//here and the difference between the integers} System.out.prin
                        TLN ("Supplement 0" +num2);
                        J1 = Num2.length ()/4;//calculates the number of cycles System.out.println (J1);
                        I1 = 0;
                            for (i = 0; i<j1;i++) {//ej2[i]=num1.substring (I, i+2); for (int j = 0; j<ej.length;j++) {System

                                . OUT.PRINTLN ("Intercepted Value:" +num2.substring (I1, i1+4));
                                System.out.println (Num1.substring (i, i+3));
                                System.out.println (Value of "ej[j]:" +ej[j]);


                 System.out.println (Num1.substring (i, i+3). Equals (Ej[j));               if (num2.substring (I1, i1+4). Equals (Ej[j)) {
                                    System.out.println ("The value of J" +j);
                                    A3 =a3+ Bj[j];

                                    I1 = I1 +4;

                                Break }} System.out.println (decimal binary hexadecimal is: +A3)
                        ; A2 = a1+ "."
                        +A3;

                    } System.out.println ("Binary hexadecimal is:" +A2); }

Haha, the above is more messy, I wrote a long time ago, there is no times to organize, to deal with integers and decimal places where there is optimization, perhaps at that time to complicate, will see. Novice Java Small white experience, there is a shortage of places, you can guide twos. Don't tell me how you don't use the function. Must be so complicated, haha. The 82 and 28 conversions are similar to the above

Related Article

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.