Chris Williams wrote:
asc() is to get the ASCII value of a character. I need to use it to get
an integer value from the Hotsync ID, that I can work with to get a
serial number. It's fairly difficult to make a serial number without
touching numbers at all.
As others have already said, in C characters *are* numbers. A char is
an integer type, just like short, int, or long.
One additional word of warning: in C, characters can be signed or
unsigned integers, just like the other integer types. This means
if you deal with characters greater than 127, you may have to take
some care to ensure they don't come out as negative numbers. For
example, if you have a character that's ASCII 130, you might end
up getting -126 if you do the naive thing:
char c = '\202'; /* octal escape code for character #130 */
int i = c;
printf ("%d\n", i);
If you want all positive numbers, the easiest thing to do is to typecast
your character to an unsigned char before converting it to another
larger integer type that can handle positive numbers larger than 128:
char c = '\202';
int i = (unsigned char) c;
printf ("%d\n", i);
That will print "130", unlike the first example, which will print "-126"[1].
- Logan
[1] Provided of course that you're on a platform that implements
printf(). Palm OS doesn't, but I'm just talking about the C
language in general here.
--
For information on using the PalmSource Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/support/forums/