Operating System - HP-UX
1753990 Members
7591 Online
108811 Solutions
New Discussion юеВ

How to convert char* to int and vice versa

 
SOLVED
Go to solution
Xiaoming Zhang
Contributor

How to convert char* to int and vice versa

How can I in c program convert a char * 's value "123" to a integer's value 123 and vice versa?

Thanks.

Xiaoming

2 REPLIES 2
RikTytgat
Honored Contributor
Solution

Re: How to convert char* to int and vice versa

Hi,

int to char*
------------
Use sprintf("%d", i);

char* to int
------------
Use i = atoi("12345");

See the manpages for details.


Hope this helps,
Rik
Dan Hetzel
Honored Contributor

Re: How to convert char* to int and vice versa

Hi,

For long integers, strtol() or ltostr() are supposed to be smaller and faster than using sprintf() for simple conversions, at least according to the manual.


Best regards,

Dan

Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com