Use Java to complete conversion of decimal to binary octal hexadecimal

Source: Internet
Author: User
Tags decimal to binary

/*

hexadecimal conversions

Requirements:

1. Input data from keyboard (with input prompt)

2. Using custom methods

Increase:

For the number of inputs, the conversion is 8-and 2-binary.

*/


Import Java.util.Scanner;

public class arraytest{

public static void Main (string[] args) {

System.out.print ("Please enter a number to convert to 16 binary:");

Scanner scan=new Scanner (system.in);

int num = Scan.nextint ();

System.out.print ("converted 16 binary number is:");

Gethex (num);

System.out.println ();

System.out.print ("Converted 2 binary number is:");

Getbinary (num);

System.out.println ();

System.out.print ("Converted 8 binary number is:");

Getoctal (num);

}

Gethex method. Complete the 16 binary conversion function.

Start Gethex;

public static void Gethex (int num) {

char[] chs={' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ',

' A ', ' B ', ' C ', ' D ', ' E ', ' F '};

char[] arr = new Char[8];

int p = arr.length; The//int type is 4 bytes in memory, 32 2 binary bits, and the corresponding hexadecimal bit is 8. Each 4 bits corresponds to one digit 16 decimal number.

if (num = = 0) {

System.out.print ("0");

return; //Return here and end this method when the received data is 0.

}

while (num! = 0) {

int x = num & 15;

ARR[--P] = chs[x]; //In the process of saving, starting from the highest bit. This is the first number to be calculated, and the last one to be stored in the array.

num = num >>> 4;   //Move num to the right by 4 2 binary bits. >>> code unsigned Right shift. >> represents a signed shift.

}

for (int i=p;i<arr.length;i++) {//The output process starts from a valid bit. With 0 not output

System.out.print (Arr[i]);

}

}

End Gethex

Start Getoctal

public static void getoctal (int num) {

char[] chs={' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 '};

Char[] Arr=new char[11];

int p=arr.length;

if (num = = 0) {

System.out.print ("0");

return;

}

while (num! = 0) {

int N=num & 7;

ARR[--P] = Chs[n];

num = num >>> 3;

}

for (int i=p;i<arr.length;i++) {

System.out.print (Arr[i]);

}

}

End Getoctal

Start Getbinary

public static void getbinary (int num) {

char[] chs={' 0 ', ' 1 '};

Char[] Arr=new char[32];

int p=arr.length;

if (num = = 0) {

System.out.print ("0");

return;

}

while (Num!=0) {

int N=num & 1;

Arr[--p]=chs[n];

num = num >>>1;

}

for (int i=p;i<arr.length;i++) {

System.out.print (Arr[i]);

}

}

End Getbinary

}


Use Java to complete conversion of decimal to binary octal hexadecimal

Related Article

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.