A string that converts a number into a 16-digit representation.

Source: Internet
Author: User
To convert a number to a 16-digit string, we can call sprintf, but the standard library of C does not provide a function that converts a hexadecimal string number to an integer number. I've written a feature that can be implemented and optimized as much as possible.
    (Currently this function only supports 32-bit)/* input hex string, format:0xaa111 or AA111 or abc11/int ahextoi (char* p) {int n = 0;

    char* q = p;

    /* Reach's Tail/while (*Q) q++;

    if (*p = = ' 0 ' && * (P + 1)!= 0)/* skip "0x" or "0X" * * + + 2;
        while (*p) {int C;
        if (*p >= ' 0 ' && *p <= ' 9 ') c = *p-' 0 ';
        else if (*p >= ' a ' && *p <= ' F ') c = *p-' a ' + 0xA;
        else if (*p >= ' a ' && *p <= ' f ') c = *p-' a ' + 0xA;

        else/* Invalid char */return 0;
        n + + C << ((int) (Q-P-1) * 4);
    p++;
} return N;

    You can test with the following code: int main (int argc, char* argv[]) {int out = 0;

    out = Ahextoi ("");

    printf ("% #010X/n", out);

    out = Ahextoi ("0x123");

    printf ("% #010X/n", out); Out = Ahextoi ("123abc");

    printf ("% #010X/n", out);

    out = Ahextoi ("ABC123");

    printf ("% #010X/n", out);

    out = Ahextoi ("0xac1b1c");

    printf ("% #010X/n", out);

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.