Convert number to characters

Implement a function to convert integer number to characters with any base. 

C++:
01 void myItoa(int num, char* A, int base)
02 {
03     int index=0;
04     int k=1;
05     if(num<0){
06         num=-num;
07         A[index++]=‘-‘;
08     }
09     while(num/k>=base)
10         k*=base;
11     while(k>=1){
12         int t=num/k;
13         A[index++]=t+‘0’;
14         num=num%k;
15         k/=base;
16     }
17     A[index++]=‘\ 0’;
18 }
Advertisements
This entry was posted in Array and linked list. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s