XINU
Functions
atoi.c File Reference

ASCII文字列をint型の整数に変換する。 More...

Go to the source code of this file.

Functions

int atoi (char *p)
 ASCII文字列をint型に変換する。 More...
 

Detailed Description

ASCII文字列をint型の整数に変換する。

Definition in file atoi.c.

Function Documentation

◆ atoi()

int atoi ( char *  p)

ASCII文字列をint型に変換する。

処理の流れは、
Step1. 「不要な文字(空白、TAB)の削除」もしくは「正数/負数の判定」を行う
Step2. 0〜9の範囲の値であれば、一桁ずつ記録する。それ以外の文字は呼び飛ばす
ASCIIコードとして、0=0x30、1=0x31、2=0x32、…、9=0x39であるため、
「0x30〜0x39の範囲のASCIIコード値(0〜9の値) - '0'(=0x30)」と計算すれば、
0〜9の整数値が得られる。
Step3. Step.1で取得した符号情報(±)を反映した整数を返す。

Parameters
[in]pASCII文字列
Returns
ASCII文字列をint型に変換した整数値
Note
ASCIIコード情報は、ターミナル上で"$ man ascii"で確認できる。

Definition at line 19 of file atoi.c.

Referenced by xsh_udpecho().

20 {
21  int n = 0, f = 0;
22 
23  for (;; p++)
24  {
25  switch (*p)
26  {
27  case ' ':
28  case '\t':
29  continue;
30  case '-':
31  f++;
32  case '+':
33  p++;
34  }
35  break;
36  }
37 
38  while (*p >= '0' && *p <= '9')
39  {
40  n = n * 10 + *p++ - '0';
41  }
42 
43  return (f ? -n : n);
44 }
Here is the caller graph for this function: