Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String to integer - Please HELP

Status
Not open for further replies.

Realazad

Programmer
Apr 5, 2002
15
US
This may probably be too trivial but I can not think anymore and I am completely stuck. This is the problem -

I am receiving a string (5 digits long) from another computer (over a socket), and I need to convert it to an integer like this -

In String- "00275"
Understand it as integer 275

and vice-versa as

Out integer - 483
send as string "00483"

I tried atoi and itoa but they strip the zeros (which I definitely need during communication).

Any help is appreciated.

Thanks
Azad
 
You already have the atoi() working ok when receiving strings : 00275 -> 275 Ok ?


An easy (but somewhat expensive) way to do it the other way :
char buf[6];
int intval = 275;
sprintf(buf, "%05d", intval);

will produce "00275" in buf (With a binary zero as terminator in the last byte).


/JOlesen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top