How to convert a string to binary, octal, decimal, or hexadecimal in C

Source: Internet
Author: User

In C language, a program needs to convert the text into a hexadecimal number. For example, if the string "0x1a" is converted into a hexadecimal 26, you can use the following function to implement
Related functions: atof, atoi, atol, strtodd, strtoul
Header file: # include <stdlib. h>
Define the function: long int strtol (const char * nptr, char ** endptr, int base );
Function Description
This function converts the nptr parameter to the Growth integer number based on the base parameter. The base parameter ranges from 2 to 36, or 0. The base parameter indicates the base method. For example, if the base value is 10, the base value is 10. If the base value is 16, the base value is hexadecimal. When the base value is 0, 10 is used for conversion, however, if the '0x 'prefix is used for hexadecimal conversion, and if the '0' prefix is not '0x', the '8' prefix is used for conversion. At the beginning, strtol () scans the nptr parameter string and skips the leading space character until it starts to convert in case of a number or positive or negative sign, when a non-number or string ends ('\ 0'), the conversion ends and the result is returned. If the endptr parameter is not NULL, the character pointer in the nptr that is terminated due to an exception is returned by endptr. If the endptr parameter is NULL, no invalid string is returned.
Strtol is an enhanced version of atoi
This is mainly reflected in the following aspects:
1. Not only can recognize decimal integers, but also other hexadecimal integers, depending on the base parameter, such as strtol ("0XDEADbeE ~~ ", NULL, 16) returns the 0xdeadbee value, strtol (" 0777 ~~ ", NULL, 8) returns the value of 0777.
2. endptr is an outgoing parameter. The function returns the first character that is not recognized. For example, char * pos; strtol ("123abc", & pos, 10);, strtol returns 123, and pos points to the letter a in the string. If no identifiable integer exists at the beginning of the string, for example, char * pos; strtol ("ABCabc", & pos, 10);, strtol returns 0, and pos points to the beginning of the string, this can be used to determine the case of this error, which is not handled by atoi.
3. if the integer in the string exceeds the long int value range (overflow or underflow), strtol returns the maximum (or least) integer it can represent and sets errno to ERANGE, for example, strtol ("0XDEADbeef ~~ ", NULL, 16) returns 0x7fffffff and sets errno to ERANGE


Convert strings a, B, and c into numbers in hexadecimal notation 10, 2, and 16, respectively.
------------------------------------------------
# Include <stdlib. h>
# Include <stdio. h>
Int main ()
{
Char a [] = "100 ";
Char B [] = "100 ";
Char c [] = "0x11 ";
Int x, y, z;
X = strtol (a, NULL, 10 );
Y = strtol (B, NULL, 2 );
Z = strtol (c, NULL, 16 );
Printf ("x = % d \ n", x );
Printf ("y = % d \ n", y );
Printf ("z = % d \ n", z );
}
Output: x = 100
Y = 4
Z = 17


For example, each character in string B occupies one Byte (1 Byte = 8 bits) space in the memory) the content in the byte is converted to a binary number (the string "100" removes double quotation marks and is a binary number 100). After that, the binary number is printed in decimal format 4.

 

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.