magdiel linhares
asked on
Convert Char to Int help!
Hello!!!!
How to convert this;
How to convert;
char time[13] = '1517691086218'
to
int value = 1517691086218
?
Thanks...
How to convert this;
std::string response;
char time[13];
...
response.copy(time, 13,223);
time[13] = '\0';
printf("%s\n" ,time); // this print value:1517691086218
???? //how to convert char time[13] to int ?
if (time == 1517691086218){
printf("OK %d\n" ,time);
} else {
printf("NOT\n %d\n" ,time);
}
How to convert;
char time[13] = '1517691086218'
to
int value = 1517691086218
?
Thanks...
Hi, I write few function to convert std::string to int
#include "iostream"
#include "string.h"
using namespace std;
int charToint(char ch)
{
switch(ch)
{
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
};
}
int pow(int n)
{
int i = 0;
int r = 1;
while(i<n)
{
r *= 10;
i++;
}
return r;
}
int atoi(string strNumber)
{
if (strNumber.length() == 0) return 0;
int i=0, j=0;
int number = 0;
for (i=0;i<strNumber.length();i++)
{
number += charToint((char)strNumber[i]) * pow(strNumber.length()-j-1);
j++;
}
return number;
}
int main(int argc, char const *argv[])
{
string strNumber;
getline(cin, strNumber);
cout<<atoi(strNumber)<<endl;
return 0;
}
@Phạm Minh Toàn:
I don't see the point in re-doing the wheel ....
@
magdiel linhares:
I didn't notice this was C++, in that case, scratch my previous answer and use the std::stoi() function (defined in the string header).
Also, since you're coding in C++, use string objects instead of those horrible C-Style arrays, and use teh std::cout object to print you datas to the standard output instead of printf.
Last, using the "using namespace std" directive is considered a bad practice. It's sole porpose is to ensure compatibility with older code base when namespaces were introduced in C++ (back in 1990), it have no business in modern codes.
Either fully qualify your function calls, or select precisely what you need:
I don't see the point in re-doing the wheel ....
@
magdiel linhares:
I didn't notice this was C++, in that case, scratch my previous answer and use the std::stoi() function (defined in the string header).
Also, since you're coding in C++, use string objects instead of those horrible C-Style arrays, and use teh std::cout object to print you datas to the standard output instead of printf.
Last, using the "using namespace std" directive is considered a bad practice. It's sole porpose is to ensure compatibility with older code base when namespaces were introduced in C++ (back in 1990), it have no business in modern codes.
Either fully qualify your function calls, or select precisely what you need:
int main()
{
std::cout << "Hello world." << std::endl;
return 0;
}
int main()
{
using std::cout;
using std::endl;
cout << "Hello world." << endl;
return 0;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@Fabrice Lambert, you're right. Thanks
ASKER
Thanks! Working :3
Use the atoi() or atol() function, wich convert an string to an integer or long respectively.
Open in new window