Skip to content

Instantly share code, notes, and snippets.

@kr4fty
Last active August 6, 2017 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kr4fty/486b6b8dfcb19d58ea6311ed042ddcd0 to your computer and use it in GitHub Desktop.
Save kr4fty/486b6b8dfcb19d58ea6311ed042ddcd0 to your computer and use it in GitHub Desktop.
itoa: int to ascii
/* ITOA()
*
* int to ascii
*
* Descripcion:
* Convierte un entero a su ascii equivalente.
* Retorna:
* un puntero a string con los caracteres numericos que componen el entero
* Ejemplo:
* buff=itoa(1024): luego en buff={'1','0','2','4','\0'}
*
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<unistd.h>
const char * itoa(int num);
void main(){
int num, num2;
const char *buff;
while(1){
num = random();
printf("%d\t-\t", num);
buff = itoa(num);
printf("%s\t", buff);
num2 = atoi(buff);
num == num2? printf("---> OK!\n"): printf("---> bad!\n");
usleep(100000);
}
}
const char * itoa(int num){
static char b[10];
unsigned long int i, k;
int j, digito;
k = 1;
while( (num/(k*10)) )
k *= 10;
for(j=0,i=k; i>0; i/=10, j++){
digito = num/i;
b[j] = '0' + digito;
num = num%i;
}
b[j]='\0';
return b;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment