Common binary conversion process (taking commercially stay I) Analytical principle, based on the stack to achieve attachment binary code conversion

Speaking of conversion between binary, with the possible exception specimens Conormal (compiled my own name, did not find the original What's the name ...) is the most common, but if we wanted to use the most commonly What principle of the method in the end what is it?

Like this hex conversion chart below, we know the decimal octal 1348 rpm, as long as he has been in addition to 8 until the quotient is zero, and the last remainder is the reverse of the results we want.

 

 

But why do not doubt that the formula we use is not correct? Where is the legality of it?

The following will prove the legality of the methods described above

 

We know that the right number of decimal value distribution is as follows:

 

 

First, we do a very boring decimal to decimal conversion, that is, the end result is the same, or the original decimal number, but how we get the numbers of the bits of it?

Consider the following procedure:

If we adopt the same method to get digital specimens from more than individual bits except: 

First 1348% 10 = 8, what does this indicate?

 

 

 

 

 

 

 

 

 This shows that our 1348 can be divided into 134 1 10 ^ 10 ^ 0 and 8, which is 10 134 and 8 1, so that we can get the number of bits on the least significant bit.

 So we can enter the number in the last position

 

 

 You may have found clever principle. If not, do not worry, we do step in, you might see out.

 

 

 

 This time we can get: 13 10 1348 is 2 ^ 10 ^ 1 and 4 and 10 ^ 8 zeros, corresponding, we can fill in the 10 ^ 4 1

 Because 13 2 10 ^ 13 is not in the single digits, so we can not fill him in on the 10 ^ 2.

 

 So how to dispose of it 13? In addition to his then 10 ^ 1

 

 

 

 At this time we can fill in the numbers on the 10 ^ 2, it is clear that the figure of 3

 And because we remainder is 1, and already a number of representatives of a 10 ^ 3, it is easy to think that this one can directly fill in the position of the 10 ^ 3

 

 Similarly, we used this idea to convert the decimal octal:

 

 

 

 

 

 

Similarly with the other band, such as binary rampant in the computer industry, too.

 

 Results We observed this hex conversion, and counted out the last figures we want is upside down, so we can use this structure to achieve what the stack binary conversion.

 First, we implement a simple stack structure: 

public class Stack{
       private Object[] objects;
       private int top = -1;
       private static final int initialSize = 32;

        public Stack() {
            this.objects = new Object[initialSize];

        }

        public <T> T pop(){ //<T>意味着我们可以根据需要选择返回的类型,不用每次获得都强转,虽然有类型转换错误的危险,但是这里是演示用,不做太严谨措施
            if(top < 0){ return null; }
            return (T) objects[top --];
        }
        public <T> T getTop(){
            if(isEmpty()){ return null; }
            return (T) objects[top];
        }
        public void push(Object o){
            objects[++ top] = o;
        }

        public boolean isEmpty(){
            return top == - 1;
        }
    }

 

 具体实现 :

public String conversion(int source, int destinRadix){
        StringBuilder builder = new StringBuilder();
        Stack stack = new Stack();

        while(source != 0){
            //如下的运算比较低效,为了演示没有考虑效率
            int left = source % destinRadix;
            source = (source - left) / destinRadix;
            stack.push(left);
        }

        while(!stack.isEmpty()){
        //如果要转10进制以上的数请自行将10定义成a,11定义成b...... builder.append(stack.pop());
}
return builder.toString(); }

测试:

 

 

 

    

Guess you like

Origin www.cnblogs.com/lqlqlq/p/12045683.html