How to convert *char into int??

Jul 3, 2011 at 7:34am
here is a source that i am working on.

class A
{
public :
char * m_pbuff;

KcFile(){m_pbuff = (char *)malloc(128);}
......
};

int main()
{
A tmp;
int *p_int, *p_float;
int a = 10; float b= 10.5;
p_int = &a; p_float = &b;
memcpy(tmp.m_pbuff, p_int, sizeof(int));
tmp.m_pbuff = m_pbuff + 4;
memcpy(tmp.m_pbuff, p_float, sizeof(float));
tmp.m_pbuff = m_pbuff + 4;

-> And then i wanna read all data in tmp.m_pbuff.
however, I think i have to convert it into proper data type.
I don't know how to do.
I need any help. thank you in advance.
}
Jul 3, 2011 at 7:52am
You could use a stringstream to convert it.
1
2
3
4
5
6
std::stringstream ss;
ss<<"3142";
int a;
if(ss>>a && ss.empty()) {
   // things are good
}
Jul 3, 2011 at 8:54am
What you are doing now is copying bytes from one place to another. The data types involved don't affect anything. I don't see what you are trying to achieve. While your title implies that you want to convert a string to an integer, you don't have a string. Is it the other way? In either case, Zhuge gave the right answer.
Jul 3, 2011 at 9:31am
that small snippet that Zhuge posted wont compile. it says it doesnt know what ss.empty is. there is no function empty in stringstream.
Jul 3, 2011 at 9:47am
It's true. stringstream has no member empty():
http://cplusplus.com/reference/iostream/stringstream/
Jul 3, 2011 at 10:50am
Topic archived. No new replies allowed.