"C language" any decimal digit to hex

Source: Internet
Author: User

Approximate idea:
Enter any decimal digit first to determine the number of digits;

uses an array of shapes to store hexadecimal digits; a[0] represents the number of digits in hexadecimal digits;

#include <stdio.h>#include<stdlib.h>#defineMAXSIZE 10int* Ahex (intDec) {    intn=1; intm= -; int*A; A= (int*)malloc(sizeof(int) *maxsize+1);  while(Dec >=m) {m= m* -; N++; }//printf ("number of positions:%d\n", N); *a =N;  while(n>0)    {         * (a+n) = dec/(m/ -); Dec= dec-* (a+n) * (m/ -); N--; M= m/ -; }    returnA;}voidPrint_hex (int*p) {    intn = *p; printf ("hex:0x");  while(n>0)    {        Switch(* (p+N)) {             Case Ten:p rintf ("A"); Break;  Case  One:p rintf ("B"); Break;  Case  A:p rintf ("C"); Break;  Case  -:p rintf ("D"); Break;  Case  -:p rintf ("E"); Break;  Case  the:p rintf ("F"); Break; default: printf ("%d", * (P+n)); Break; } N--; } printf ("\ n");}intMain () {intRec=0; int*p;  while(1) {printf ("Please enter a decimal number:"); scanf ("%d",&rec); P=Ahex (REC);    Print_hex (P); }    return 0;}

Final effect:

"C language" any decimal digit to hex

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.