Java implementation decimal number to hexadecimal number

Source: Internet
Author: User
Tags stringbuffer

Now~let ' s begin our second question~

How do I use the Java language to convert decimal numbers to hexadecimal digits?

The first time I coded the effect was Jiangzi ~

/** *  */ PackageCom.succez.task2;ImportJava.util.Scanner;/*** <p>copyright:copyright (c) 2018</p> * <p>succez</p> *@authorZhangjinjin * @createdate May 3, 2018*/ Public classInttohex {/*** First enter an integer number to determine whether it is 0, if it is 0, then its 16 binary is also 0; * If number is not 0, then 16 is taken and converted to 16 binary corresponding character; * NUMBER=NUMBER/16, repeat process 2, 3, with character array s depending on Times to save each bit; * Output when the reverse output can be*/     Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); intNumber =Sc.nextint (); inti = 0; Char[] S =New Char[100]; if(Number = = 0) {System.out.print (0); }        Else{             while(number!=0)              {                intT=number%16; if(t >=0 && t<10) {S[i]= (Char) (t+ ' 0 '); I++; }                Else{S[i]= (Char) (t+ ' A '-10); I++; } Number=number/16; }                          for(intj=i-1;j>=0;j--) {System.out.print (s[j]); }        }    }}

Of course It ' s true~

As can see from the picture~

However, after discussion, we found that although this can achieve its function, but it takes a lot of brains to think, then, it is clear to express it clearly ~

So, after some modification, this should be OK ~

/** *  */ PackageCom.succez.task2;ImportJava.util.Scanner;/*** <p>copyright:copyright (c) 2018</p> * <p>succez</p> *@authorZhangjinjin * @createdate May 4, 2018*/ Public classInttohex_alter {/*** This algorithm uses StringBuffer to be more efficient.*/     Public Static voidMain (string[] args) {//TODO auto-generated Method StubSystem.out.println ("Please enter the number of decimal to convert:"); Scanner input=NewScanner (system.in); intn =Input.nextint (); System.out.println ("The number of hexadecimal conversions is:" +Inttohex (n)); }    Private StaticString Inttohex (intN) {stringbuffer s=NewStringBuffer ();        String A; Char[]b = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '};  while(n! = 0) {s= S.append (b[n%16]); N= N/16; } A=s.reverse (). toString (); returnA; }}

Hehe ~ has been submitted, ok~

Let's take a look at StringBuffer

The addition of the string type is not very efficient, so this time it is necessary to consider the StringBuffer inside the Append () this method ~

OK, absolutely perfect ~

Java implementation decimal number to hexadecimal number

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.