Hi all,
I need to convert a char array to an int. But the char array has regular characters in them('A', 'B', 'C'...) and numbers.

how would I omit those characters and extract the number, then store it as an int.

any help would be great

Does that string represent hex numbers? such as "0A" ? If yes then you don't want to skip the alpha characters. Call strtol() to make the conversion. The last parameter to that function should be 16 (base 16 is hexidecimal).

Thanks for your reply,

but no the string dosn't represent hex numbers.
the format of the string is two uppercase letters and 8 numbers. "XX000000" or "VD12345678".
I need get the last 8 digits and store it as an int.

thanks again, but I've figured it out.
But im not sure if Im doing it the correct way.
Is it good practice to do it like this, or would there be a beter way?

char *charArray = "XX12345678";
	int id = atoi(&charArray[2]);
	printf("\n%d\n", id);

wht abt this...... more clear

int chartoint(char s[]) {
   int i,no=0;

   for(i=0;s[i]!=0;i++)               // for every char in the string
      if(str[i]>='0' && str[i]<='9')  // if it is a digit
         no=no*10+str[i]-48;          // add it to the end of the number
   return no; 
int main{
   printf("%d", chartoint(charArray));


I like the idea of going through each char, more reusable than my solution.

you could also use isdigit() to check if its a number.

also is that the correct way of looping to the end of an array in c?


sorry I mainly use java and c++.

I am not sure whether ths s da correct way...

acrdng to my knoledge i dd ths

may b there are some other ways........ thts doesnt mater


or else u can do with strlen


or else u can do with strlen


You could, but you should never use strlen(s) in a loop like that unless the length of the string changes somewhere inside that loop. Why? Because the program has to call strlen() function on every loop iteration, whould could be very very time consuming, making your program slow.

Instead, either check for the string's null terminating character as previously posted, or set an int to the value returned by strlen() and use it in the loop.

int len = strlen(s);
for(i = 0; i < len; i++)