Blue Bridge Cup hex to decimal (string)

Source: Internet
Author: User
Tags hex to decimal pow time limit
Basic practice hex-to-decimal time limit: 1.0s memory Limit: 512.0MB problem description Enter a positive hexadecimal number string that does not exceed 8 bits from the keyboard, converting it to a positive decimal number after the output.
Note: 10~15 in hexadecimal numbers are denoted by uppercase letters A, B, C, D, E, F, respectively. Sample input FFFF sample output 65535 because the problem requires an input string, it cannot be entered as%x directly, and the output is%d, which is not 100% correct.
#include <stdio.h>
#include <string.h>
#include <math.h>
int main ()
{
    char a[ ];
    scanf ("%s", a);
    int L=strlen (a);
    A long long ans=0;//note is a positive hexadecimal number string of no more than 8 bits, so the number is larger and should be set to long long for
    (int i=0; i<l; i++)
    {
        if (a[i]>= ' 0 ') &&a[i]<= ' 9 ')
            ans+= (a[i]-' 0 ') *pow (16*1.0,l-1-i);
        else
            ans+= (a[i]-' a ' +10) *pow (16*1.0,l-1-i);
    }
    printf ("%lld\n", ans);
    return 0;
}

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.