-
Notifications
You must be signed in to change notification settings - Fork 170
new page strtol.md
#1475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
new page strtol.md
#1475
Changes from all commits
090865f
ea1f776
a32b5ce
3e711d8
2bc7cf6
95a1cf0
a3c4ce4
5b1c95f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,92 @@ | ||||||||||||||
# strtol | ||||||||||||||
* cstdlib[meta header] | ||||||||||||||
* std[meta namespace] | ||||||||||||||
* function[meta id-type] | ||||||||||||||
|
||||||||||||||
```cpp | ||||||||||||||
namespace std { | ||||||||||||||
long int strtol(const char* nptr, char** endptr, int base); | ||||||||||||||
} | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
## 概要 | ||||||||||||||
文字列`nptr`を`long`型の整数に変換する。文字列は`base`で指定された基数に従って解釈される。 | ||||||||||||||
|
||||||||||||||
`endptr`が非`nullptr`の場合、変換が終了した位置の文字へのポインタがそこに格納される。 | ||||||||||||||
|
||||||||||||||
基数`base`は 2~36 または 0 の値を取る。 | ||||||||||||||
|
||||||||||||||
`base`が0の場合 | ||||||||||||||
- 文字列の先頭が`0x`または`0X`のときは16進数 | ||||||||||||||
- 文字列の先頭が`0`のときは8進数 | ||||||||||||||
- その他のときは10進数として変換する。 | ||||||||||||||
|
||||||||||||||
変換は次のように行われる | ||||||||||||||
- 先頭が空白文字の場合、最初の非空白文字から変換される。 | ||||||||||||||
- `+`または`-`が先頭にある場合は、符号として解釈される。 | ||||||||||||||
- 基数が 16 または 0 の場合`0x`または`0X`をスキップする | ||||||||||||||
- その後の文字列を、指定された`base`に基づいて整数値に変換する。 | ||||||||||||||
- 各桁は、`0`〜`9`をその値、`a`〜`z`および`A`〜`Z`を10〜35として扱う。 | ||||||||||||||
|
||||||||||||||
## 戻り値 | ||||||||||||||
- 変換可能ならば変換後の数値。 | ||||||||||||||
- 変換後の数値が`long`の範囲外の場合、`LONG_MAX`または `LONG_MIN`。 | ||||||||||||||
K10-K10 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
- 変換不可能なら`0`を返す。 | ||||||||||||||
|
||||||||||||||
## 備考 | ||||||||||||||
- 数値が `long` の範囲を超えるときは `errno` に `ERANGE` が設定される。 | ||||||||||||||
|
||||||||||||||
## 例 | ||||||||||||||
```cpp example | ||||||||||||||
#include <iostream> | ||||||||||||||
#include <cstdlib> | ||||||||||||||
#include <cerrno> | ||||||||||||||
#include <climits> | ||||||||||||||
|
||||||||||||||
void convert_and_print(const char* str, int base) { | ||||||||||||||
errno = 0; | ||||||||||||||
char* end = nullptr; | ||||||||||||||
long result = std::strtol(str, &end, base); | ||||||||||||||
|
||||||||||||||
std::cout << "基数" << base << "での変換結果: " << result << std::endl; | ||||||||||||||
if (str == end) { | ||||||||||||||
std::cout << "変換不可" << std::endl; | ||||||||||||||
} else if (errno == ERANGE) { | ||||||||||||||
std::cout << " → 変換結果が範囲外" << std::endl; | ||||||||||||||
} | ||||||||||||||
if (*end != '\0') { | ||||||||||||||
akinomyoga marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
std::cout << "未変換部分: \"" << end << "\"" << std::endl; | ||||||||||||||
} | ||||||||||||||
std::cout << std::endl; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
int main() { | ||||||||||||||
const char* str = " -0x2Fabc"; | ||||||||||||||
convert_and_print(str, 0); // 自動判別 | ||||||||||||||
convert_and_print(str, 10); // 10進数 | ||||||||||||||
convert_and_print(str, 36); // 36進数 | ||||||||||||||
|
||||||||||||||
return 0; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
``` | ||||||||||||||
### 出力 | ||||||||||||||
``` | ||||||||||||||
基数0での変換結果: -195260 | ||||||||||||||
|
||||||||||||||
基数10での変換結果: 0 | ||||||||||||||
未変換部分: "x2Fabc" | ||||||||||||||
|
||||||||||||||
基数36での変換結果: -1999456248 | ||||||||||||||
|
||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
## 関連項目 | ||||||||||||||
akinomyoga marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
- [`strtoll`](strtoll.md.nolink): 文字列を、基数を指定して`long long`型に変換する | ||||||||||||||
- [`strtoul`](strtoul.md.nolink): 文字列を、基数を指定して`unsigned long`型に変換する | ||||||||||||||
- [`strtoull`](strtoull.md.nolink): 文字列を、基数を指定して`unsigned long long`型に変換する | ||||||||||||||
Comment on lines
+85
to
+87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 関連項目の部分の記述は、 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cstdlib の方の説明でそうなっているのには気づいていませんでした。 特に合わせなければならないということもないように思います。現に、既存のページの関連項目で、そのページの関数とリンク先のページの関数の「関係性・違い」を中心にした説明にしている場合も多くあるように思います。例えばシンプルに " cstdlib の説明の方を見ると「文字列を~」というのが並んでいるので、その並び方との一貫性で「文字列を」から始めているのは理解できるような気がします。一方で、今回の文脈では、「文字列を、基数を指定して |
||||||||||||||
- [`std::stol`](/reference/string/stol.md): 文字列から`long`型への変換 | ||||||||||||||
- [`<charconv>` ヘッダ](/reference/charconv.md): 高速な文字列 ⇔ 数値変換 | ||||||||||||||
|
||||||||||||||
## 参考文献 | ||||||||||||||
- Qiita: [Cのstrtolの使い方とendptrの活用例(@yumetodo)](https://qiita.com/yumetodo/items/238751b879c09b56234b) |
Uh oh!
There was an error while loading. Please reload this page.