### Author Topic: C programming Int to ASCII c-style string  (Read 11884 times)

0 Members and 1 Guest are viewing this topic.

#### BS403

• 29
• I'm just sitting in my Cave.
##### C programming Int to ASCII c-style string
Ok I need a little help.  I need to write a function that takes in an int and a pointer and takes the in and write it to the pointer as ascii values.  I have the function almost completed, I just need to know how do I convert the individual numbers from the int into ascii.  My first guess is to add 48 to them based on the ascii tables i have found.  I've done this before in assembly, but I can't remember if its hex 48 or decimal 48 that I need to add.
Any help would be appreciated.

EDIT: It was decimal 48 or 0x30. Thanks anyway

« Last Edit: April 08, 2009, 11:52:23 pm by BS403 »
http://woogleville.myminicity.com/

Homer: Aw, twenty dollars! I wanted a peanut!
Homer's Brain: Twenty dollars can buy many peanuts.
Homer: Explain how.
Homer's Brain: Money can be exchanged for goods and services.
Homer: Woo-hoo!

#### blackhole

• Still not over the rainbow
• 29
• Destiny can suck it
##### Re: C programming Int to ASCII c-style string
Well the usual solution to this is the itoa function, but since this is an assignment, your probably giong to end up re-implementing it. In that case, you'll want to isolate each individual digit of the input int, then, like you said, add 48 to it, convert it to char value and add it to the string pointer.

#### Wanderer

• Wiki Warrior
• 211
• Mostly harmless
##### Re: C programming Int to ASCII c-style string
umm.. assuming just a single digit...
char c = int i + '0' ?
Do not meddle in the affairs of coders for they are soggy and hard to light

#### portej05

• 29
##### Re: C programming Int to ASCII c-style string
Is this taking in a number and turning it into a string?

I'm not sure I get the question

But well done on solving it!
STRONGTEA. Why can't the x86 be sane?

#### Flipside

• əp!sd!l£
• 212
##### Re: C programming Int to ASCII c-style string
Yup, just add the correct amount to the ASCII value of the number, it's like converting upper to lower case, all you need to do is deduct the difference in ASCII values between 'A' and 'a', which is 32 iirc.

#### Wanderer

• Wiki Warrior
• 211
• Mostly harmless
##### Re: C programming Int to ASCII c-style string
Dunno how much does it matter but i seem to recall being warned that using fixed ASCII codes (say assuming that 48 is always '0') is rather practice as it may not be the same result on all systems.. That is IIRC only things that are 'guaranteed' are that numbers follow each other in the 'ASCII series' as do the characters.
Do not meddle in the affairs of coders for they are soggy and hard to light

#### Flipside

• əp!sd!l£
• 212
##### Re: C programming Int to ASCII c-style string
Well, if it doesn't follow the ASCII coding scheme, it isn't ASCII, it's a variation of it, but, strictly speaking, the 'S' in ASCII is for 'standard', and that is what it really should be.

Yes, that's not really the recommended method on most systems these days, which seem to consider such a simple solution to be too 'low level', which is silly, and that it assumes too much about the system being used, which isn't so silly, but for a quick and dirty solution, that is a viable technique.

#### blackhole

• Still not over the rainbow
• 29
• Destiny can suck it
##### Re: C programming Int to ASCII c-style string
Dunno how much does it matter but i seem to recall being warned that using fixed ASCII codes (say assuming that 48 is always '0') is rather practice as it may not be the same result on all systems.. That is IIRC only things that are 'guaranteed' are that numbers follow each other in the 'ASCII series' as do the characters.

If 48 doesn't equal '0' then it isn't ASCII, it's something else, and in the context of an assignment, if they specify "ASCII" then you can easily make that assumption. In the real world, you have to deal with Unicode and other scary things.

#### BS403

• 29
• I'm just sitting in my Cave.
##### Re: C programming Int to ASCII c-style string
the assignment was actually to implement my own version of the itoa function.  I got it figured out. Thanks for all the replies.

umm.. assuming just a single digit...
char c = int i + '0' ?

Yeah that would have been the easy way for me to do it.
« Last Edit: April 08, 2009, 11:52:07 pm by BS403 »
http://woogleville.myminicity.com/

Homer: Aw, twenty dollars! I wanted a peanut!
Homer's Brain: Twenty dollars can buy many peanuts.
Homer: Explain how.
Homer's Brain: Money can be exchanged for goods and services.
Homer: Woo-hoo!

#### Bobboau

• Just a MODern kinda guy
Just MODerately cool
And MODest too
• 213
##### Re: C programming Int to ASCII c-style string
Dunno how much does it matter but i seem to recall being warned that using fixed ASCII codes (say assuming that 48 is always '0') is rather practice as it may not be the same result on all systems.. That is IIRC only things that are 'guaranteed' are that numbers follow each other in the 'ASCII series' as do the characters.

which is why you don't use 48 but rather 'a', let the compiler figure it out for you.

as for the question, off the top of my head, I'm thinking something like

while(number){
string+='0'+(number%10);
number/=10;
}

except that would have all the characters backwards.

so maybe a recursive solution

string itoa(num){
if(num)
return itoa(num/10)+('0'+(num%10));
else
return "";
}

yeah, I think that will do it, of course I don't think I'm using correct syntax, but it should be a decent enough guide.
Bobboau, bringing you products that work... in theory
learn to use PCS
creator of the ProXimus Procedural Texture and Effect Generator
My latest build of PCS2, get it while it's hot!
PCS 2.0.3

DEUTERONOMY 22:11
Thou shalt not wear a garment of diverse sorts, [as] of woollen and linen together