C 文字列操作 新しいページはコチラ
提供: yonewiki
(→文字列の検索と置換) |
(→文字列と数値の変換) |
||
4,435行: | 4,435行: | ||
=='''文字列と数値の変換'''== | =='''文字列と数値の変換'''== | ||
実際のアプリケーションでは、数値の入力を行う部分でも、文字コードとして入力がなされ、各種のエディタや、エディットコントロール、入力画面からは文字としてテキストから渡されます。このように取り扱われる文字列としての数値を、プログラミング言語の数を扱う型に格納する技術が必要になります。 | 実際のアプリケーションでは、数値の入力を行う部分でも、文字コードとして入力がなされ、各種のエディタや、エディットコントロール、入力画面からは文字としてテキストから渡されます。このように取り扱われる文字列としての数値を、プログラミング言語の数を扱う型に格納する技術が必要になります。 | ||
+ | |||
+ | atoi、atol、atofのような標準関数で簡単に文字列を数値に変換することができます。逆に数値を文字列に変換する関数もitoaやltoa、ftoaのように準備されていますが非標準関数となっています。ほぼ標準で準備されています。同じことがprintf関数のような関数のvsprint関数を使って実現できます。こちらの方が応用が利くので、自分ならこういうものを活用します。atoiのような関数はC++ではstd::atoiのような名前空間の中に定義されています。使い方は同じです。 | ||
+ | |||
+ | では、以下がサンプルになります。 | ||
+ | <syntaxhighlight lang="cpp" line start="1"> | ||
+ | #pragma once | ||
+ | #include "stdafx.h" | ||
+ | #include <stdlib.h> | ||
+ | #include <string> //std::string型定義 | ||
+ | #include <mbstring.h> //mbs***関数 | ||
+ | #include <iostream> //cpp cout etc 一般入出力関数 | ||
+ | #include <locale> //Locale関数 | ||
+ | #include <tchar.h> //TCHAR型+_tcs***関数 | ||
+ | #include "atlstr.h" //CString,CStringA,CStringWおよびLPTSTR,LPWSTR,LPSTR,LPCTSTR,LPCWSTR,LPCSTRの定義プリプロセッサ | ||
+ | #include "atlbase.h" //同上 | ||
+ | #include "cstringt.h" //CStringTの定義プリプロセッサ | ||
+ | #include <vector> | ||
+ | |||
+ | |||
+ | //_bstr_t型を利用するプリプロセッサ | ||
+ | #include "comutil.h" | ||
+ | #ifdef _DEBUG | ||
+ | #pragma comment(lib, "comsuppwd.lib") | ||
+ | #else | ||
+ | #pragma comment(lib, "comsuppw.lib") | ||
+ | #endif | ||
+ | //_bstr_t | ||
+ | |||
+ | //ICU ucnvプリプロセッサ | ||
+ | #include <unicode/ucnv.h> | ||
+ | #include <unicode/translit.h> | ||
+ | #include <unicode/regex.h> | ||
+ | #ifdef _DEBUG | ||
+ | //#pragma comment(lib, "icudt.lib") | ||
+ | #pragma comment(lib, "icuind.lib")//ICU Transliterate関数を使うために必要なライブラリ 正規表現関数も | ||
+ | #pragma comment(lib, "icuucd.lib")//ICU ucnvを使うために必要なライブラリ | ||
+ | //#pragma comment(lib, "icuiod.lib") | ||
+ | #else | ||
+ | #pragma comment(lib, "icudt.lib") | ||
+ | #pragma comment(lib, "icuind.lib") | ||
+ | #pragma comment(lib, "icuuc.lib") | ||
+ | #pragma comment(lib, "icuio.lib") | ||
+ | #endif | ||
+ | |||
+ | |||
+ | #include "UnicodeConverter.h" | ||
+ | |||
+ | |||
+ | using namespace std; | ||
+ | int getvscprintflen(char* format, ...){ | ||
+ | va_list args; | ||
+ | int len; | ||
+ | |||
+ | //可変長引数をargsに格納。引数formatの次から全てをva_list型のargsへ設定 | ||
+ | //引数文字列ポインタをformatの次に合わせる関数。 | ||
+ | va_start( args, format ); | ||
+ | len = _vscprintf( format, args ); | ||
+ | return len; | ||
+ | } | ||
+ | |||
+ | void getvsprintf(char* str, char* format, ...){ | ||
+ | va_list args; | ||
+ | int len; | ||
+ | |||
+ | va_start( args, format ); | ||
+ | len = _vscprintf( format, args ); | ||
+ | vsprintf_s( str, len + 1, format, args ); | ||
+ | } | ||
+ | int _tmain(int argc, _TCHAR* argv[]) | ||
+ | { | ||
+ | char pcStrInt[]="-8192"; | ||
+ | const char pcStrFloat[]="-3.14"; | ||
+ | int nInteger; | ||
+ | long lLong; | ||
+ | double dDouble; | ||
+ | char* pcStrNumeric; | ||
+ | |||
+ | int nLen; | ||
+ | nInteger = atoi(pcStrFloat); | ||
+ | lLong = atol(pcStrFloat); | ||
+ | dDouble = atof(pcStrInt); | ||
+ | |||
+ | printf("★文字列の数値化 型違いの場合\n"); | ||
+ | printf("nInteger->%d\n",nInteger); | ||
+ | printf("lLong ->%d\n",lLong); | ||
+ | printf("dDouble ->%f\n",dDouble); | ||
+ | printf("\n"); | ||
+ | |||
+ | nInteger = atoi(pcStrInt); | ||
+ | lLong = atol(pcStrInt); | ||
+ | dDouble = atof(pcStrFloat); | ||
+ | |||
+ | printf("★文字列の数値化 型一致の場合\n"); | ||
+ | printf("nInteger->%d\n",nInteger); | ||
+ | printf("lLong ->%d\n",lLong); | ||
+ | printf("dDouble ->%f\n",dDouble); | ||
+ | printf("\n"); | ||
+ | |||
+ | |||
+ | //文字列長を取得する | ||
+ | nLen = getvscprintflen("%3.2f", dDouble ); | ||
+ | |||
+ | pcStrNumeric = new char[nLen + 1]; | ||
+ | |||
+ | //文字列を出力※同じ引数を2回与えているのは冗長なので | ||
+ | //pcStrNumericをメンバ関数にしたクラスを作ると一度にまとめれる。 | ||
+ | getvsprintf( pcStrNumeric, "%3.2f", dDouble ); | ||
+ | |||
+ | printf("★数値を文字列 vsprintf関数を利用する場合\n"); | ||
+ | printf( "%s\n", pcStrNumeric); | ||
+ | |||
+ | delete[] pcStrNumeric; | ||
+ | return 0; | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | 出力結果 | ||
+ | <syntaxhighlight lang="text"> | ||
+ | ★文字列の数値化 型違いの場合 | ||
+ | nInteger->-3 | ||
+ | lLong ->-3 | ||
+ | dDouble ->-8192.000000 | ||
+ | |||
+ | ★文字列の数値化 型一致の場合 | ||
+ | nInteger->-8192 | ||
+ | lLong ->-8192 | ||
+ | dDouble ->-3.140000 | ||
+ | |||
+ | ★数値を文字列 vsprintf関数を利用する場合 | ||
+ | -3.14 | ||
+ | </syntaxhighlight> | ||
=='''文字列と日付・日時・時間の変換'''== | =='''文字列と日付・日時・時間の変換'''== |