Re: [CODE] Tried and failed this function...

From: Daniel Koepke (
Date: 11/27/98

Christoffer Lundberg wrote:
> Sorry didn't pass compilation. It slung a pair of
> (x3) "integers without a cast" right onto my screen.
> Well...can I make it in any easier way?

atoi() accepts a 'char *' (i.e., a string) not a 'char' (i.e., a single
character), which is why you are receiving that error. The best code to
do this conversion has already been posted.

  a = *buf - '0';
  b = *(buf+1) - '0';
  c = *(buf+2) - '0';

It's very simple to understand, if you know anything about the ASCII
character set. Quickly, each character (alphanumerics, punctuation,
etc.) are given a decimal representation. This is called their "ASCII
code". Fortunately for programmers, ASCII is laid out logically. That
is, '0' comes right before '1', '1' before '2', '2' before '3', and so
on, just as one would expect. Thus, between '1' and '0' there is a
difference of 1. Between '2' and '0' there is a difference of 2. I'm
sure you've caught on by now.

So, in the above code, if buf = "125", then we do,

  a = '1' - '0' = 1
  b = '2' - '0' = 2
  c = '5' - '0' = 5

Please note that the single quotes are required. The ASCII code of
'0' is not 0, it's something else entirely (I don't know what off the
top of my head, though). More examples of doing these nifty character
manipulations can be seen in CircleMUD's LOWER/UPPER macros, where it
takes advantage of the fact that 'a'-'z' are in order, as well as

If the above doesn't give the desired output, there are two

  1. You aren't using the ASCII character set (in which case, LOWER
     and UPPER shouldn't work, either). This is pretty unlikely, since
     most platforms--AFAIK--use ASCII, and if you aren't, then Circle
     is probably broken in more ways than

  2. Your buffer isn't being filled properly, a rather important thing
     you've neglected to comment upon up to this point. That is, if
     you aren't properly storing your data into your buffer, then you
     are *converting* the contents of the buffer fine, just the
     contents of the buffer aren't what you think they are.


     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | |

This archive was generated by hypermail 2b30 : 12/15/00 PST