:: reverse dictionary ::
※ソースファイルについて
n進数の文字列を数値に変換するには、strtol 関数を使います。
#include <stdlib.h>
charstr[] = "11";char*endstr;longbase2, base8, base16; base2 = strtol(str, &endstr, 2); /* 2進数 */ base8 = strtol(str, &endstr, 8); /* 8進数 */ base16 = strtol(str, &endstr, 16); /* 16進数 */
base2 は 3 、 base8 は 9 、 base16 は 17 になります。
ワイド文字列の場合は wcstol 関数を使います。
#include<locale.h>#include<stdlib.h>
wchar_t str[] = L"11";
wchar_t *endstr;
long base2, base8, base16;
setlocale(LC_ALL, "ja"); /* ロケールを日本語に設定 */
base2 = wcstol(str, &endstr, 2); /* 2進数 */
base8 = wcstol(str, &endstr, 8); /* 8進数 */
base16 = wcstol(str, &endstr, 16); /* 16進数 */
base2 は 3 、 base8 は 9 、 base16 は 17 になります。
longstrtol(constchar*s,char**e,intr)longwcstol(constwchar_t *s, wchar_t **e,intr)
文字列 s を 基数 r で数値に変換します。
1文字も変換できなかった場合は 0 を返します。
数値に変換できる所までが変換対象となり、変換できなかった場合はその文字へのポインタを e に設定します。
すべての文字を変換できた場合は、e に "" (空の文字列)を設定します。
s … 数値に変換する文字列
e … 変換できなかった文字へのポインタ
r … 基数
文字列 s を基数 r で変換した数値
(1文字も変換できなかった場合は 0 )
基数とは、数値の各桁の基礎になる数です。
(「n進数」の「n」の部分)
最大36進数まで使うことができます。
(0 〜 9 の数字と a 〜 z のアルファベット26文字)
charstr[] = "z";char*endstr;longbase36; base36 = strtol(str, &endstr, 36);
base36 は 35 になります。
ワイド文字列も同じです。
wchar_t str[] = L"z";
wchar_t *endstr;
long base36;
setlocale(LC_ALL, "ja"); /* ロケールを日本語に設定 */
base36 = wcstol(str, &endstr, 36);
base36 は 35 になります。
int 型の数値(整数)に変換するlong 型の数値(整数)に変換するdouble 型の数値(小数点数)に変換する
Copyright (C) 2005-2007 Noto Watabe. All rights reserved.
e-mail:wmh@always-pg.com