XINU
Functions
atol.c File Reference

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

Go to the source code of this file.

Functions

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

Detailed Description

ASCII文字列をlong型の実数に変換する。

Definition in file atol.c.

Function Documentation

◆ atol()

long atol ( char *  p)

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

処理の流れは、
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文字列をlong型に変換した実数
Note
ASCIIコード情報は、ターミナル上で"$ man ascii"で確認できる。

Definition at line 19 of file atol.c.

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