Jump to content

C++ Programming/Code/Standard C Library/Functions/itoa

From Wikibooks, open books for an open world
Syntax
/* Convert a decimal integer to a c-style string */
char* itoa(int a, char b[]);

The itoa function converts a integer argument and stores that in char array b[]. This function is not very fast or concise compared to other itoa implementations, but illustrates a byte level implementation of itoa in C++ without using standard library.

/*
Name : itoa.cpp
Description:
Program to convert a integer to a c-style string.
Author: AM
Date : 3-4-2015
Revision and bug history:
Version : 0.1
License: GNU GPLv3

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/

#include "iostream"

using namespace std;

/* Convert a decimal integer to a c-style string */
char* itoa(int a, char b[]);

/* Get number of digits in an integer */
int getNumofDecDigit(int Number);

/* get a character from  digit input number */
char getStrChar(int digit);

/* Application program */
int main (void)
{
    int Number =0;
    char Str[10] = "";
    cout << "Enter a positive or negative integer with upto 8 digits: " ;
    cin >> Number;
    itoa(Number, Str);
    cout << "Displaying in string form: " << Str;
    return 0;
}

/* Convert a decimal integer to a c-style string */
char* itoa(int a, char b[])
{
   int NumofDigits = getNumofDecDigit(a);
    if(a>0)
    {
    switch(NumofDigits)
    {
        case 1:
        b[0]=getStrChar(a);
        b[1]='\0';
        break;

        case 2:
        b[0]= getStrChar(a/10);
        b[1]= getStrChar(a%10);
        b[2]='\0';
        break;

        case 3:
        b[0]= getStrChar(a/100);
        b[1]= getStrChar((a/10)%10);
        b[2]= getStrChar(a%10);
        b[3]='\0';
        break;

        case 4:
        b[0]= getStrChar(a/1000);
        b[1]= getStrChar((a/100)%10);
        b[2]= getStrChar((a/10)%10);
        b[3]= getStrChar(a%10);
        b[4]='\0';
        break;

        case 5:
        b[0]= getStrChar(a/10000);
        b[1]= getStrChar((a/1000)%10);
        b[2]= getStrChar((a/100)%10);
        b[3]= getStrChar((a/10)%10);
        b[4]= getStrChar(a%10);
        b[5]='\0';
        break;

        case 6:
        b[0]= getStrChar(a/100000);
        b[1]= getStrChar((a/10000)%10);
        b[2]= getStrChar((a/1000)%10);
        b[3]= getStrChar((a/100)%10);
        b[4]= getStrChar((a/10)%10);
        b[5]= getStrChar(a%10);
        b[6]='\0';
        break;

        case 7:
        b[0]= getStrChar(a/1000000);
        b[1]= getStrChar((a/100000)%10);
        b[2]= getStrChar((a/10000)%10);
        b[3]= getStrChar((a/1000)%10);
        b[4]= getStrChar((a/100)%10);
        b[5]= getStrChar((a/10)%10);
        b[6]= getStrChar(a%10);
        b[6]='\0';
        break;

        case 8:
        b[0]= getStrChar(a/10000000);
        b[1]= getStrChar((a/1000000)%10);
        b[2]= getStrChar((a/100000)%10);
        b[3]= getStrChar((a/10000)%10);
        b[4]= getStrChar((a/1000)%10);
        b[5]= getStrChar((a/100)%10);
        b[6]= getStrChar((a/10)%10);
        b[7]= getStrChar(a%10);
        b[8]='\0';
        break;

        default:
        b[0]='\0';
    }
    }
    else
    {
    switch(NumofDigits)
    {
        case 1:
        b[0]='-';
        b[1]=getStrChar(a);
        b[2]='\0';
        break;

        case 2:
        b[0]='-';
        b[1]= getStrChar(a/10);
        b[2]= getStrChar(a%10);
        b[3]='\0';
        break;

        case 3:
        b[0]='-';
        b[1]= getStrChar(a/100);
        b[2]= getStrChar((a/10)%10);
        b[3]= getStrChar(a%10);
        b[4]='\0';
        break;

        case 4:
        b[0]='-';
        b[1]= getStrChar(a/1000);
        b[2]= getStrChar((a/100)%10);
        b[3]= getStrChar((a/10)%10);
        b[4]= getStrChar(a%10);
        b[5]='\0';
        break;

        case 5:
        b[0]='-';
        b[1]= getStrChar(a/10000);
        b[2]= getStrChar((a/1000)%10);
        b[3]= getStrChar((a/100)%10);
        b[4]= getStrChar((a/10)%10);
        b[5]= getStrChar(a%10);
        b[6]='\0';
        break;

        case 6:
        b[0]='-';
        b[1]= getStrChar(a/100000);
        b[2]= getStrChar((a/10000)%10);
        b[3]= getStrChar((a/1000)%10);
        b[4]= getStrChar((a/100)%10);
        b[5]= getStrChar((a/10)%10);
        b[6]= getStrChar(a%10);
        b[7]='\0';
        break;

        case 7:
        b[0]='-';
        b[1]= getStrChar(a/1000000);
        b[2]= getStrChar((a/100000)%10);
        b[3]= getStrChar((a/10000)%10);
        b[4]= getStrChar((a/1000)%10);
        b[5]= getStrChar((a/100)%10);
        b[6]= getStrChar((a/10)%10);
        b[7]= getStrChar(a%10);
        b[8]='\0';
        break;

        case 8:
        b[0]='-';
        b[1]= getStrChar(a/10000000);
        b[2]= getStrChar((a/1000000)%10);
        b[3]= getStrChar((a/100000)%10);
        b[4]= getStrChar((a/10000)%10);
        b[5]= getStrChar((a/1000)%10);
        b[6]= getStrChar((a/100)%10);
        b[7]= getStrChar((a/10)%10);
        b[8]= getStrChar(a%10);
        b[9]='\0';
        break;

        default:
        b[0]='\0';
        break;
    }

    }
    if (a==0)
    {
        b[0]= '0';
        b[1]='\0';
    }

  return b;
}

/* Get number of digits in a positive or negative integer */
int getNumofDecDigit(int Number)
{
   int NumofDigits=0;
    if(Number < 0)
      {
          Number=-Number;
      }
      while(Number)
      {
          Number/=10;
          NumofDigits++;
      }
    return NumofDigits;
}

/* Get a character from  digit input number */
char getStrChar(int digit)
{
   char  StrChar = ' ';
    switch (digit)
    {
        case 0:
        StrChar = '0';
        break;

        case 1:
        case -1:
        StrChar ='1';
        break;

        case 2:
        case -2:
        StrChar = '2';
        break;

        case 3:
        case -3:
        StrChar = '3';
        break;

        case 4:
        case -4:
        StrChar = '4';
        break;

        case 5:
        case -5:
        StrChar = '5';
        break;

        case 6:
        case -6:
        StrChar = '6';
        break;

        case 7:
        case -7:
        StrChar = '7';
        break;

        case 8:
        case -8:
        StrChar = '8';
        break;

        case 9:
        case -9:
        StrChar = '9';
        break;

        default:
        StrChar ='0';
        break;
    }
    return StrChar;
}
Related topics
atoi