/**
*
*/
Package com.dreamly.day01;
/**
* @author dreamly
*
*/
public class Arraytest {
public static void Main (string[] args) {
ToBin (6);
ToO (60);
Tohex (60);
}
/**
* Decimal Turn binary
* @param num
* @param base
* @param offset
*/
public static void toBin (int num) {
Trans (num,1,1);
}
/**
* Decimal Turn octal
* @param num
* @param base
* @param offset
*/
public static void ToO (int num) {
Trans (num,7,3);
}
/**
* Decimal Turn hex
* @param num
* @param base
* @param offset
*/
public static void Tohex (int num) {
Trans (num,15,4);
}
/**
*
* 0 1 2 3 4 5 6 7 8 9 A B C D E F = = elements in hexadecimal
* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
*
* Check table method to deal with the conversion of binary
* Store all the elements temporarily, establish correspondence, each time the value of &base as the index to see the established table, you can find the corresponding element
* This is easier than -10+ ' a '
*
* The result is reversed, is coming over can be done by stringbuffer reverse function
* Storage can also be done through an array container
*
* @param num
* @param base
* @param offset
*/
public static void Trans (int num,int base, int offset) {
if (num==0) {
SYSTEM.OUT.PRINTLN (0);
return;
}
Char[] chs= {' 0 ', ' 1 ', ' 2 ', ' 3 '
, ' 4 ', ' 5 ', ' 6 ', ' 7 '
, ' 8 ', ' 9 ', ' A ', ' B '
, ' C ', ' D ', ' E ', ' F '};
Char[] Arr=new char[32]; When instantiated: The default value in the heap space is the initial value of ' \u0000 ', that is, the empty compartment
int pos=arr.length; Pointer, where the expression is used to achieve the purpose
while (num!=0) {
int temp=num&base;
ARR[--POS]=CHS[TEMP];
num=num>>>offset;
}
for (int x=pos;x<arr.length;x++) {
System.out.print (Arr[x]);
}
System.out.println ();
}
}
Java Learning Transformation (pointer thinking, understanding Mastery)