如何将"015A"这16进制的字符串变换成int类型?
atoi函数好像不行,谁给点代码,多谢!

解决方案 »

  1.   


    char str[5] = "015A"; //346
    char c;
    int Num = 0;
    int length = strlen(str);
    int i; for (i=0; i<length; i++)
    {
    c = str[i];
    Num = (Num<<4)|(hex2int(c));
    }
    int hex2int(char c)
    {
    if(c >= '0' && c <= '9')
    return c - '0';
    if(c >= 'A' && c <= 'F')
    return c - 'A' + 10;
    if(c >= 'a' && c <= 'f')
    return c - 'a' + 10;
    return -1;
    }
      

  2.   

    strtoul, wcstoul
    Convert strings to an unsigned long-integer value.unsigned long strtoul( const char *nptr, char **endptr, int base );unsigned long wcstoul( const wchar_t *nptr, wchar_t **endptr, int base );Routine Required Header Compatibility 
    strtoul <stdlib.h> ANSI, Win 95, Win NT 
    wcstoul <stdlib.h> or <wchar.h> ANSI, Win 95, Win NT 
    For additional compatibility information, see Compatibility in the Introduction.LibrariesLIBC.LIB Single thread static library, retail version 
    LIBCMT.LIB Multithread static library, retail version 
    MSVCRT.LIB Import library for MSVCRT.DLL, retail version 
    Return Valuestrtoul returns the converted value, if any, or ULONG_MAX on overflow. strtoul returns 0 if no conversion can be performed. wcstoul returns values analogously to strtoul. For both functions, errno is set to ERANGE if overflow or underflow occurs.ParametersnptrNull-terminated string to convert endptrPointer to character that stops scanbaseNumber base to useResEach of these functions converts the input string nptr to an unsigned long. strtoul stops reading the string nptr at the first character it cannot recognize as part of a number. This may be the terminating null character, or it may be the first numeric character greater than or equal to base. The LC_NUMERIC category setting of the current locale determines recognition of the radix character in nptr; for more information, see setlocale. If endptr is not NULL, a pointer to the character that stopped the scan is stored at the location pointed to by endptr. If no conversion can be performed (no valid digits were found or an invalid base was specified), the value of nptr is stored at the location pointed to by endptr.wcstoul is a wide-character version of strtoul; its nptr argument is a wide-character string. Otherwise these functions behave identically.Generic-Text Routine MappingsTCHAR.H Routine  _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined 
    _tcstoul strtoul strtoul wcstoul  
    strtoul expects nptr to point to a string of the following form:[whitespace] [{+ | –}] [0 [{ x | X }]] [digits]A whitespace may consist of space and tab characters, which are ignored; digits are one or more decimal digits. The first character that does not fit this form stops the scan. If base is between 2 and 36, then it is used as the base of the number. If base is 0, the initial characters of the string pointed to by nptr are used to determine the base. If the first character is 0 and the second character is not 'x' or 'X', the string is interpreted as an octal integer; otherwise, it is interpreted as a decimal number. If the first character is '0' and the second character is 'x' or 'X', the string is interpreted as a hexadecimal integer. If the first character is '1' through '9', the string is interpreted as a decimal integer. The letters 'a' through 'z' (or 'A' through 'Z') are assigned the values 10 through 35; only letters whose assigned values are less than base are permitted. strtoul allows a plus (+) or minus (–) sign prefix; a leading minus sign indicates that the return value is negated.