Use sprintf to format.

Code:
sprintf (strResult, "%03s", strIpSegment);
if IpSegment is '12', strResult will be '012'.