Thread: how to convert int to char

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    29

    how to convert int to char

    Heyy

    i know its possieble to convert char to int with atoi

    but is it possible to do it the other way ...

    int --> char ??????????????

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Lets take
    Code:
     int dig = 123;
    to convert it you should pic 3, 2 and 1 seperately. Using this
    Code:
     
    
    int countVar=dig;
    
    for(int numOfDigits=0; countVar !=0;numOfDigits++) 
    	countVar=countVar/10;
    	
    char char_array[100];  //Or you can use malloc(): char* char_array=(char*)malloc(numOfDigits+1);
    	
    for(int i=numOfDigits-1;i>=0;i--)
    {
          char_array[numOfDigits-i-1]=char(dig%int(pow(10,(i+1)))/pow(10,i))+'0'; //Plus '0' to convert int to char
    } 
    char_array[numOfDigits]=NULL;
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Tell me if it didn't work.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    It's outdated C++ code on a C forum.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Not to mention the rounding errors (and poor performance) when using pow()

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    it didnt work.. sorry... but thanks anyway

  8. #8
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Quote Originally Posted by Salem
    Not to mention the rounding errors (and poor performance) when using pow()
    No problem when base is 10.
    >> It doesn't work
    To make it work first it should be in C code not C++, second math.h should be included.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Before this poor thread acquires any further confusion,
    Quote Originally Posted by Dave_Sinkula
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       char text[11];
       int value = 123;
       sprintf(text, "%d", value);
       printf("text = \"%s\"\n", text);
       return 0;
    }
    
    /* my output
    text = "123"
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    it worked perfect... thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM