Promoting char to int

This is a discussion on Promoting char to int within the C Programming forums, part of the General Programming Boards category; Hey, I'm reading in a bunch of numbers into a char array, and then I want to perform mathematical operations ...

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    Promoting char to int

    Hey,

    I'm reading in a bunch of numbers into a char array, and then I want to perform mathematical operations on these numbers.
    I've read the FAQ > Explanations of... > Definition of EOF and how to use it effectively where they talk about the promotion, but I'm still not sure if I can do what I want to do. Or maybe there's another way.
    say I have and input like so:
    126

    I read it in one character at a time. so a string would contain:
    1 2 6

    Now I want to assign the value 126 to an int. In the code count represents the number of characters read in. I can't read in the characters differently because the file contains letters and numbers and the reading function reads one char at a time.
    Code:
    int ConvertNum(FILE *inp, char *string, int count)
    {
    	int i /*position*/, j =0 /*power*/, num =0, temp;
    
    	for (i=(count-1); i >=0; i--)
    	{
    		temp = string[i];
    		num = num + temp * (10^j);
    		j++;
    
    		printf("temp = %d, string[i] = %c  \n", temp, string[i]);
    	}
    
    	return num;
    }
    thanks.

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    The atoi() function turns a string made of number characters [0-9] into an integer. That what you want?

  3. #3
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Quote Originally Posted by jim mcnamara
    The atoi() function
    That's exactly what I want.
    Thanks,

    A.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    Just my 2 cents. Not really sure if this is what u r looking for.

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	char arr[] = {'1','2','3'};
    	
    	char num[10];
    
    	int i,number;
    	
    	strncpy(num,arr,3);
    		
    	number=atoi(num);
    	printf("Number: %d\n",number);
    
    	
    	return 0;
    }
    $ ./test1.exe
    Number: 123

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    	num = num + temp * (10^j);
    ^ is the bitwise XOR operator. You are looking for the pow function.

  7. #7
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Thanks,

    Got it working,

    A.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 03:24 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21