본문 바로가기

C programming

[strtol] Converting a string to an integer 3

#include <stdio.h>
#include <stdlib.h>
//78

void main(void)
{
	char *string = "0xFF";
	char *stop; //pointer value to the conversion end position can use this to the length of the converted digits
	int radix;
	long value;
	
	radix = 16;
	
	value = strtol(string, &stop, radix);
	
	printf("%d characters have been converted\n", stop - string);
	printf("The converted value of the hexadecimal %s is %ld\n", string, value);
	
}

long int strtol(const char *str, char **endptr, int base) converts the initial part of the string in str to a long int value according to the given base, which must be between 2 and 36 inclusive or be the special value 0.

 

Parameters

  • str − This is the string containing the representation of an integral number.
  • endptr − This is the reference to an object of type char*, whose value is set by the function to the next character in str after the numerical value.
  • base − This is the base, which must be between 2 and 36 inclusive, or be the special value 0.

Return Value

This function returns the converted integral number as a long int value, else zero value is returned.