Convert a numeric string of super long type to a binary string

The specific idea is:

1. Convert the string to a character array

2. Convert character array to integer array

3. Design a method for dividing an integer array by 2

4. Design the method of taking the remainder of 2 for integer arrays

5. Design the exit condition, the value of all elements of the integer array is 0

Don't say anything, just post the code.

public class BigIntToBin {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String intstr="8392162";
        String result="";
        char [] charArray=StrToCharArray(intstr);
        int [] intArray=CharArrayToIntArray(charArray);
        boolean exitFlag=false;
        
        while(true){
            exitFlag=checkExit(intArray);
            if(exitFlag){
                break;
            }else{
                result=String.valueOf(intArrayModTwo(intArray))+result;
                intArray = IntArrayDivTwo (intArray);
            }
        }
        System.out.println(result);

    }

    // String to character array 
    static  char [] StrToCharArray(String str){
         return str.toCharArray();
    }
    
    // Character array to integer array 
    static  int [] CharArrayToIntArray( char [] charArray){
         int [] intArray= new  int [charArray.length];
         for ( int i = 0; i < intArray.length; i++ ) {
            intArray[i] = charArray[i] - '0';
        }
        return intArray;
    }
    
    // Check if all values ​​of integer string are 0 
    static  boolean checkExit( int [] intArray){
         int sum = 0 ;
         for ( int i = 0; i < intArray.length; i++ ) {
            sum=sum+intArray[i];
        }
        if(sum==0){
            return true;
        }else{
            return false;
        }
    }
    
    // Integer array divided by 2 to get a new array 
    static  int [] IntArrayDivTwo( int [] intarray){
         int [] resultArray= new  int [intarray.length];
         boolean giveFlag= false ;
        
        // Traverse the integer array and calculate the division by 2 
        for ( int i = 0; i < intarray.length; i++ ) {
             // Complement processing 
            if (giveFlag){
                resultArray[i] = (int)((10+intarray[i])/2);
                giveFlag=((10+intarray[i])%2)==1?true:false;
            } else {
                 // The last bit is processed 
                if (i== intarray.length){
                    resultArray[i]=(int)(intarray[i]/2);
                } else {
                     // The value is 1 to process 
                    if (intarray[i]==1 ){
                        resultArray[i]=0;
                        giveFlag = true ;
                     // The value is not 1 
                    } else {
                        resultArray[i] = (int)(intarray[i]/2);
                        giveFlag = (intarray[i]%2)==1?true:false;
                    }
                }
            }
        }
        return resultArray;
    }

    // Integer array takes the remainder of 2 
    static  int intArrayModTwo( int [] intarray){
         return intarray[intarray.length-1]%2 ;
    }
}

Unhandled exceptions, as a tester, my goal is to implement functionality without considering empty strings, strings with all 0s, etc.

After this processing, adding large numbers, subtracting large numbers, and multiplying large numbers is not a problem. There are still many things to consider when dividing large numbers, and they have not been sorted out yet.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325235994&siteId=291194637