PDA

View Full Version : Convert array of charts to int



Kimm
March 1st, 2007, 05:57 PM
I'm trying to write a program that would read every line of a file to an array of integers (1 line to 1 position in the integer).

My Program reads the file but as a list of charts.

How do I write char temp[3] = "342" to int x?

Tomosaur
March 1st, 2007, 06:17 PM
Well, it depends on which language you're writing it in :P

Wybiral
March 1st, 2007, 07:57 PM
Assuming it's C/C++, use the "atoi" function.

Here's an example in C:


#include <stdio.h>

int main()
{
char* strValue = "1004";
int intValue = atoi(strValue);
intValue *= 2;
printf("%s * 2 = %i \n", strValue, intValue);
}


In C++:


#include <iostream>
#include <string>

using namespace std;

int main()
{
string strValue = "1004";
int intValue = atoi(strValue.c_str());
intValue *= 2;
cout << strValue << " * 2 = " << intValue << endl;
}


In python:


strValue = "1004"
intValue = int(strValue)
intValue *= 2
print strValue, " * 2 = ", intValue

Kimm
March 1st, 2007, 10:54 PM
Assuming it's C/C++, use the "atoi" function.

Here's an example in C:


#include <stdio.h>

int main()
{
char* strValue = "1004";
int intValue = atoi(strValue);
intValue *= 2;
printf("%s * 2 = %i \n", strValue, intValue);
}


In C++:


#include <iostream>
#include <string>

using namespace std;

int main()
{
string strValue = "1004";
int intValue = atoi(strValue.c_str());
intValue *= 2;
cout << strValue << " * 2 = " << intValue << endl;
}


In python:


strValue = "1004"
intValue = int(strValue)
intValue *= 2
print strValue, " * 2 = ", intValue


Oops, sorry. Forgot to say what language I was writing it in. But I was using C++ so thanks for the advice! :D