Convert Char to Int help!


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'


int value = 1517691086218


magdiel linharesAsked:
Who is Participating?
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

Fabrice LambertFabrice LambertCommented:

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

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)
		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;
		r *= 10;
	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);
	return number;

int main(int argc, char const *argv[])
	string strNumber;
	getline(cin, strNumber);
	return 0;

Open in new window

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

Phạm Minh ToànStudentCommented:
@Fabrice Lambert, you're right. Thanks
magdiel linharesAuthor Commented:
Thanks! Working :3
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.