how to convert int to char

This is a discussion on how to convert int to char within the C Programming forums, part of the General Programming Boards category; Heyy i know its possieble to convert char to int with atoi but is it possible to do it the ...

  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,006
    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,231
    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,231
    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,006
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    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,231
    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,006
    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, 06:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21