※前提条件:本情報はUbentu15.04を基づいて説明してる char*型の変換 †//ヘッダーファイル #include <stdlib.h> //intへキャスト const char *chrptr = "100"; int t1 = atoi(chrptr); std::cout << "t1 = " << t1 << std::endl; //longへキャスト long t2 = atol(chrptr); std::cout << "t2 = " << t2 << std::endl; //long longへキャスト long long t3 = atoll(chrptr); std::cout << "t3 = " << t3 << std::endl; int型の変換 †//ヘッダーファイル #include <stdlib.h> int intValue = 100; char tchrptr[10]; //ubentuにitoa関数を提供していないため、sprintf関数を利用することになる。 //itoa(intValue,tchrptr,10); sprintf(tchrptr, "%d", intValue); //#include <iostream> std::cout << "tchrptr = " << tchrptr << std::endl; string型/char*型の変換 †string型をchar*型へ変換 //ヘッダーファイル #include <string.h> std::string strValue = "abc"; const char *chrt1 = strValue.c_str(); //#include <iostream> std::cout << "chrt1 = " << chrt1 << std::endl; char*型をstring型へ変換 //ヘッダーファイル #include <string.h> const char *chrt1 = "abc"; std::string strValue ; strValue = chrt1; //#include <iostream> std::cout << "strValue = " << strValue << std::endl; const char*型をchar*型へ変換 const char* cpc ="abc"; char* pc =new char[100]; strcpy(pc,cpc,sizeof(cpc)); string型/intの変換 †int型をstring型へ変換 int intValue = 10; std::stringstream os; os << intValue; std::cout << "os = " << os.str() << std::endl; string型をintへの変換 //ヘッダーファイル #include <stdlib.h> int intValue = 100; string tchrptr= std::stoi(intValue ); std::cout << "tchrptr = " << tchrptr << std::endl; 超多機能のアクセス解析が永久無料! コメント: |