Java Learning Transformation (pointer thinking, understanding Mastery)

Source: Internet
Author: User

/**
*
*/
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)

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.