Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple int to char[] conversion

I have a simple question

How to simply convert integer (getting values 0-8) to char, e.g. char[2] in C?

Thanks

like image 786
Waypoint Avatar asked Feb 19 '11 09:02

Waypoint


2 Answers

main()
{
  int i = 247593;
  char str[10];

  sprintf(str, "%d", i);
// Now str contains the integer as characters
}

Hope it will be helpful to you.

like image 182
Yoko Zunna Avatar answered Nov 11 '22 15:11

Yoko Zunna


#include<stdio.h> 
#include<stdlib.h>
#include<string.h>
void main()
{
int a = 543210 ;
char arr[10] ="" ;

itoa(a,arr,10) ;   // itoa() is a function of stdlib.h file that convert integer 
                   // int to array itoa( integer, targated array, base u want to             
                   //convert like decimal have 10 

for( int i= 0 ; i < strlen(arr); i++)   //  strlen()   function in string file thar return string length
  printf("%c",arr[i]);

}
like image 21
prashant00101 Avatar answered Nov 11 '22 14:11

prashant00101