Converting a C integer to character

  • JohnyNFullEffect
  • Born
  • Born
  • JohnyNFullEffect
  • Posts: 2
  • Loc: Mt. Pleasant, MI

Post 3+ Months Ago

Ok. I've been scanning this site along with every other site google gives me about converting a C int into a char. I need to convery a digit into a char array. so "1" an int, into a char array in c[0], for example. PLEASE HELP!!!!

I've tried:

sprintf, *itoa, casting, changing to ASCII....

with sprintf, it does the conversion but for some odd reason, changes the way my program works, so I can't use it. *itoa doesn't work period. casting gives me a blank char insead of making the int in to a char. Changing the int to the ASCII value does nothing. It keeps it as the same digit for some reason. PLEASE HELP ME!

Here is a code segment:

/* code to add a string of digits in a char array to another
char array and place them back in a char array. Used for
grossly long integers that cannot be put in the int var.

ex: "113423423423423423423423434" + "3434343434343"

add each digit separtly and put into a result char array
*/

for(i = 0; i < strlen(a); i++){
char firsta = a[i];
int tempa = atoi(&firsta);

char firstb = b[i];
int tempb = atoi(&firstb);

result = (tempa + tempb + carry) % 10;
carry = (tempa + tempb + carry) / 10;

/* Insert conversion from int to char here */

printf("result:%d, carry:%d\n", result, carry);
}


Thank you for your help.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • JohnyNFullEffect
  • Born
  • Born
  • JohnyNFullEffect
  • Posts: 2
  • Loc: Mt. Pleasant, MI

Post 3+ Months Ago

I fixed this problem but I encountered another:

I am using the toascii() function with the #include <ctype.h> file. I compile with gcc and the flag -Wall and it shows:

warning: implicit declaration of function `toascii'

I have included everything I need and it still says that. WTF?

Please help! Thanks!
  • stinger
  • Graduate
  • Graduate
  • stinger
  • Posts: 157
  • Loc: San Jose, CA

Post 3+ Months Ago

Hello,

Let me verify what you want to do:

You have a integer, lets call it myInt.
so. .

myInt = 123456789

and you want this to become a array? Like this?

myIntArray[0] = 1
myIntArray[1 = 2
myIntArray[2 = 3
myIntArray[3 = 4
myIntArray[4 = 5
myIntArray[5 = 6
and so on. . . . ???
Is this correct? Are you using a int, double, or long int? as your number?
There is a simpler method for this.

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 94 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.