Hard Light Productions Forums
Off-Topic Discussion => Programming => Topic started by: BS403 on April 08, 2009, 03:15:02 am
-
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 :D
-
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.
-
umm.. assuming just a single digit...
char c = int i + '0' ?
-
Is this taking in a number and turning it into a string?
I'm not sure I get the question :P
But well done on solving it!
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.