Decimal and binary conversion (Java)

Binary Decimal turn

public String tenTotwo(int N) {        
    String str = "";        
    int x = N;        
    int y = 0;        
    while(x != 0){            
        y = x % 2;            
        x = x / 2;            
        str = y + str;        
    }        
    return str;    
}

Binary Coded Decimal

public int twoToten(String two) {        
    int n = 0;        
    int j = 1;        
    for(int i = two.length() - 1; i >= 0; i--){            
        int x = two.charAt(i) - 48;            
        n = n + x * j;            
        j = j * 2;        
    }        
    return n;    
}
Published 35 original articles · won praise 2 · Views 959

Guess you like

Origin blog.csdn.net/y18771025420/article/details/103378172