Convert Char to Int help!

Hello!!!!


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);
	}

Open in new window



How to convert;

char time[13] = '1517691086218'

to

int value = 1517691086218

?

Thanks...
magdiel linharesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fabrice LambertFabrice LambertCommented:
hi,

Use the atoi() or atol() function, wich convert an string to an integer or long respectively.
#include <stdlib.h>

int value = atoi(time);

Open in new window

0
Phạm Minh ToànStudentCommented:
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;
}

Open in new window

0
Fabrice LambertFabrice LambertCommented:
@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:
int main()
{
    std::cout << "Hello world." << std::endl;
    return 0;
}

int main()
{
    using std::cout;
    using std::endl;

    cout << "Hello world." << endl;
    return 0;
}

Open in new window

0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

sarabandeCommented:
int value = 1517691086218

the number isn't an 'int' but a 'long long' or '_int64' type. therefore  std:stoi would fail (and throw an exception out-of-range)

you could use std::stoll  which would convert to long long but i fear that it might fail as well if response is formatted and contains a \n linefeed character at end.
a good c++ way is to use std::stringstream for conversion

#include <string>
#include <sstream>

// you need a size of 14 if the maximum string length is 13 such that the terminating zero character could be added
char time[13+1] =  "1517691086218";

// now the following statement is valid and would not write beyond array boundaries. 

time[13] = '0';  
// however it is not needed if you initialize the char array with a literal or like: 
char s[255] = { 0 };
// the latter would initialize the whole array with zero characters

// std::string automatically would resize if the right char array is properly terminated with a zero.
std::string response = time;

// create a istringstring and seed it with the response string
std::istringstream iss(response); 
long long i64_response = 0;
if (iss >> i64_response)
{
     // now i64_resonse contains  1517691086218
     ....
}

// the same you get by 
size_t endpos = 0;
i64_response = std::stoll (response, &endpos, 10);

if (endpos <= response.length())
{
    // error: the number ends before string length ==> there is a non-digit at response[endpos]; 

 

Open in new window


Sara
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Phạm Minh ToànStudentCommented:
@Fabrice Lambert, you're right. Thanks
0
magdiel linharesAuthor Commented:
Thanks! Working :3
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.