Hexadecimal---decimal conversion

Source: Internet
Author: User

16 or 16---> 10

Idea: The hexadecimal string is treated as a decimal string, followed by a method of converting a decimal string to a number, from a high to a position-by-character basis.

Code:

#include <stdio.h> #include <string.h>/* the value of M as needed to handle the 16-bit characters of more digits */#define M 10/* processing string, returning 10 binary digits */long long Toten (char s[]) {int I;/*long long to accommodate a larger number, if the data is too large, it is necessary to use array processing */long long d=0;    for (i=0; s[i]; i++)    {/    *     d = d * 10 increases the number of bits in D, leaving a position for the currently processed character */        d=d*16;        /*        if s[i] is 0 ~ 9 digits, directly plus s[i]-' 0 ' *         /if (s[i]>= ' 0 ' && s[i]<= ' 9 ')            d=d+s[i]-' 0 ';        /* Letters to be case-sensitive *        /else if (s[i]>= ' a ' && s[i]<= ' F ')        {            d=d+s[i]-' a ' +10;        }        else if (s[i]>= ' a ' && s[i]<= ' F ')        {            d=d+s[i]-' a ' +10;        }    }    return D;} int main () {char str[m];gets (str);p rintf ("%lld", Toten (str)); return 0;}

Two or 10--16

Idea: simple addition to K-take-rest method

Code:

#include <stdio.h>/* to handle up to 50 decimal digit strings, change */#define M 50int Main () {int I=0,j,len,temp;long Num;char str[m] as needed; scanf ("%ld", &num);d o{/* except for the K-remainder */temp = (num%16); */* The number *//* remainder of the 16 binary is determined according to remainders less than 10, plus the character ' 0 ' */if (temp<10) {Str[i] = temp+ ' 0 '; }else{/* remainder greater than 10, plus character ' a ' (can also be small A */str[i] = temp-10 + ' a ';} i++;/* except K rounding */num/=16;} while (num);/* Flashback output array str for each character */for (j=i-1;j>=0;j--) {printf ("%c", Str[j]);} return 0;}


Hexadecimal---decimal conversion

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.