Typ konvertierung

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Kirjuxa
Posts: 13
Joined: Thu Oct 15, 2009 8:55 am

Typ konvertierung

Post by Kirjuxa »

Hi Leute,

also ich bin Anfänger in Irrlicht und mehr oder weniger in C++.

Mein grosses Problem war immer den Fehler meiner "Mini"- Games zu finden und deshalb wollte ich diese immer meine Variablenwerte im Fenster ausgeben lassen.
Allerdings hat das mit der Konvertierung der einzelnen Typen nicht immer gepasst denn ich wusste nicht immer welchen Befehl ich für eine Umwandlung brauche.

Deshalb habe ich eine TypeConverter Klasse geschrieben, die erstmal die gängigsten Typen in einander umwandeln kann. Auch für Irrlicht der so oft gebrauchte wchar_t- Typ ist dabei.
Ich weiss, dass man die Konvertierung eigentlich selbst vornehmen kann aber falls jemand den C++ Befehl davon nicht kenn

hier der link: http://rapidshare.de/files/48773458/Con ... r.rar.html

ich hoffe es hilft einigen.
damn, it never works
Bear_130278
Posts: 237
Joined: Mon Jan 16, 2006 1:18 pm
Location: Odessa,Russian Federation

Post by Bear_130278 »

Все понятно 8))
Do you like VODKA???
Image
Image
Bate
Posts: 364
Joined: Sun Nov 01, 2009 11:39 pm
Location: Germany

Post by Bate »

Bien entendu
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Das deutsche Irrlicht-Forum ist hier: http://www.irrlicht3d.de/
Ansonsten würde es auch helfen, den Code direkt zu posten, statt RapidShare zu bemühen, bzw. den Vorteil gegenüber compile time casts zu verdeutlichen (ich befürchte nämlich, dass dies hier deutlich langsamer ist als unbedingt nötig)

----
Short summary in English
This is a "type conversion utility" which seems to be used in place of casts. Didn't check the code, though, so don't nail me on this.
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Post by randomMesh »

This is the type converter he meant.

Some noticeable things:
1. 'using namespace std;' in a header is a bad idea
2. mixed C and C++ (string.h instead of cstring, TypeConverter(void), etc)
3. i miss const-correctness :(
4. inconsistent code style

TypeConverter.h

Code: Select all

#pragma once
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <sstream>
#include <string>
using namespace std;
class TypeConverter
	{
	public:
		TypeConverter(void);
		~TypeConverter(void);
		void set_intToCharSize(int size);
		int get_intToCharSize();
		//INTEGER
		char* intToChar(int* item);
		const char* intToConstChar(int* item);

		wchar_t* intToWChar(int* item);
		const wchar_t* intToConstWChar(int* item);

		string intToString(int* item);
		const string intToConstString(int* item);

		double intToDouble(int* item);
		const double intToConstDouble(int* item);

		float intToFloat(int* item);
		const float intToConstFloat(int* item);

		__int64 intToInt64(int* item);
		const __int64 intToConstInt64(int* item);


		//~INTEGER

		//INTEGER64
		char* int64ToChar(__int64* item);
		const char* int64ToConstChar(__int64* item);

		wchar_t* int64ToWChar(__int64* item);
		const wchar_t* int64ToConstWChar(__int64* item);

		double int64ToDouble(__int64* item);
		const double int64ToConstDouble(__int64* item);

		int int64ToInt(__int64* item);
		const int int64ToConstInt(__int64* item);

		string int64ToString(__int64* item);
		const string int64ToConstString(__int64* item);

		float int64ToFloat(__int64* item);
		const float int64ToConstFloat(__int64* item);
		//~INTEGER64

		//DOUBLE
		char* doubleToChar(double* item);
		char* doubleToChar(double* item, int vorkomma, int nachkomma,bool nachkomma_kuerzen);
		const char* doubleToConstChar(double* item);
		const char* doubleToConstChar(double* item,int vorkomma,int nachkomma,bool nachkomm_kuerzen);

		wchar_t* doubleToWChar(double* item);
		const wchar_t* doubleToConstWChar(double* item);

		string doubleToString(double* item,int nachkomma);
		const string doubleToConstString(double* item,int nachkomma);

		int doubleToInt(double* item);
		const int doubleToConstInt(double* item);

		__int64 doubleToInt64(double* item);
		const __int64 doubleToConstInt64(double* item);

		float doubleToFloat(double* item);
		const float doubleToConstFloat(double* item);
		//~DOUBLE
		//FLOAT
		 
		char* floatToChar(float* item);
		const char* flaotToConstChar(float* item);

		wchar_t* floatToWChar(float* item);
		const wchar_t* floatToConstWChar(float* item);

		string floatToString(float* item, int nachkomma);
		const string floatToConstString(float* item, int nachkomma);

		int floatToInt(float* item);
		const int floatToConstInt(float* item);

		__int64 floatToInt64(float* item);
		const __int64 floatToConstInt64(float* item);

		double floatToDouble(float* item);
		const double floatToConstDouble(float* item);
		//~FLOAT
        //CHAR
		int charToInt(char* item);
		const int charToConstInt(char* item);

		__int64 charToInt64(char* item);
		const __int64 charToConstInt64(char* item);

		float charToFloat(char* item);
		const float charToConstFloat(char* item);

		double charToDouble(char* item);
		const double charToConstDouble(char* item);

		string charToString(char* item);
		const string charToConstString(char* item);

		wchar_t* charToWChar(char* item);
		const wchar_t* charToConstWChar(char* item);

		//~CHAR
		//WCHAR_T

		int wchar_tToInt(wchar_t* item);
		const int wchar_tToConstInt(wchar_t* item);

		__int64 wchar_tToInt64(wchar_t* item);
		const __int64 wchar_tToConstInt64(wchar_t* item);

		float wchar_tToFloat(wchar_t* item);
		const float wchar_tToConstFloat(wchar_t* item);

		double wchar_tToDouble(wchar_t* item);
		const double wchar_tToConstDouble(wchar_t* item);

		string wchar_tToString(wchar_t* item);
		const string wchar_tToConstString(wchar_t* item);

		char* wchar_tToChar(wchar_t* item);
		const char* wchar_tToConstChar(wchar_t* item);
		//~WCHAR_T
		//STRING
		int stringToInt(string* item);
		const int stringToConstInt(string* item);

		__int64 stringToInt64(string* item);
		const __int64 stringToConstInt64(string* item);

		float stringToFloat(string* item);
		const float stringToConstFloat(string* item);

		double stringToDouble(string* item);
		const double stringToConstDouble(string* item);

		char* stringToChar(string* item);
		const char* stringToConstChar(string* item);

		wchar_t* stringToWchar_T(string* item);
		const wchar_t* stringToConstWchar_T(string* item);

		//~STRING

		void ShowTest();
	private:
		int intToCharSize;
		char* char_buffer;
		wchar_t* wchar_t_buffer;
		char* wchar_c_buffer;
		
		static const int Wchar_c_buffer_Size = 5000;
		static const int CharOfIntBufferSize = 5000;
		int int1;
		int a;
		__int64 int64;
		__int64 int642;
		double doub;
		double doub2;
		float flo;
		char* satz;
		char* ch;
		wchar_t* wchar;
		wchar_t* wsatz;
		string str;
		string str2;



		
	};

TypeConverter.cpp

Code: Select all

#include "StdAfx.h"
#include "TypeConverter.h"
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <sstream>
#include <iostream>
#include <new.h>
#include <float.h>
using namespace std;
TypeConverter::TypeConverter(void)
	{
		char_buffer = new char[CharOfIntBufferSize];
		wchar_t_buffer = new wchar_t[CharOfIntBufferSize];
		wchar_c_buffer = new char[Wchar_c_buffer_Size];

		
 int1 = 1234567890;
 int64= 1556667778881234124;
 int642 = 7777777777777;
 a = 54321;
 doub = 987654321;
 doub2 = 12345.578901;

 flo = 4352.192837475f;

 satz = new char[50];
 satz = "Hallo, das ist nur ein Test!";
 ch = new char[15];
 ch = "244336.123";

 wchar = new wchar_t[20];
 wchar = L"70605.04030201";

 wsatz = new wchar_t[50];
 wsatz = L"Hallo, das ist nur ein wchar_t Test!";
 
  str = "123456.765123456789";
  str2 = "Hallo, das ist nur ein string Test!";
		
	}

TypeConverter::~TypeConverter(void)
	{
		delete this->char_buffer;
		delete this->wchar_t_buffer;	
		delete this->wchar_c_buffer;

		this->int1 = 0;
		this->a = 0;
		this->int64 = 0;
		this->int642 = 0;
		this->doub = 0;
		this->doub2 = 0;
		this->flo = 0;
		delete this->satz;
		delete this->ch;
		delete this->wchar;
		delete this->wsatz;
		this->str = "";
		this->str2 = "";
		
	}

//INTTO
char* TypeConverter::intToChar(int *item)
	{
	_itoa_s(*item, char_buffer, CharOfIntBufferSize, 10);

		return char_buffer;
	}

const char* TypeConverter::intToConstChar(int* item)
	{
	_itoa_s(*item,char_buffer,CharOfIntBufferSize,10);
	return char_buffer;
	}

wchar_t* TypeConverter::intToWChar(int *item)
	{
	
	_itow_s(*item,wchar_t_buffer,11,10);
	return wchar_t_buffer;
	}

const wchar_t* TypeConverter::intToConstWChar(int* item)
	{
	return intToWChar(item);
	}

string TypeConverter::intToString(int* item)
	{
		stringstream _ss;
	if(_ss<< *item)
		{
		return _ss.str();
		}
	else
		{
		return 0;
		}
	}

const string TypeConverter::intToConstString(int* item)
	{
	return (intToString(item));
	}

double TypeConverter::intToDouble(int* item)
{
	return (double)*item;
}

const double TypeConverter::intToConstDouble(int* item)
{
	return (double)*item;
}

float TypeConverter::intToFloat(int* item)
{
	return (float)*item;
}

const float TypeConverter::intToConstFloat(int* item)
{
	return (float)*item;
}

__int64 TypeConverter::intToInt64(int* item)
{
	return (__int64)*item;
}

const __int64 TypeConverter::intToConstInt64(int* item)
{
	return (__int64)*item;
}
//~INTTO
//INT64TO
char* TypeConverter::int64ToChar(__int64 *item)
{
	
	_i64toa_s(*item,char_buffer,CharOfIntBufferSize,10);
	return char_buffer;
}

const char* TypeConverter::int64ToConstChar(__int64 *item)
{
	
	_i64toa_s(*item,char_buffer,CharOfIntBufferSize,10);
	return char_buffer;
}

wchar_t* TypeConverter::int64ToWChar(__int64* item)
{


	_i64tow_s(*item,wchar_t_buffer,CharOfIntBufferSize,10);
	return wchar_t_buffer;
}

const wchar_t* TypeConverter::int64ToConstWChar(__int64* item)
{
	return int64ToWChar(item);	
}

double TypeConverter::int64ToDouble(__int64 *item)
{
    return (double)*item;
}

const double TypeConverter::int64ToConstDouble(__int64 *item)
{
	return (double)*item;
}

int TypeConverter::int64ToInt(__int64 *item)
{
	stringstream ss;
	char* buffer = new char[10];
	if(ss << *item)
	{
		for(int i = 0;i<9;i++)
		{
			buffer[i] = ss.str()[i];
		}
		buffer[9] = '\0';
	}
	unsigned int result = atoi(buffer);

	return result;	
}

const int TypeConverter::int64ToConstInt(__int64 *item)
{
	return int64ToInt(item);
}

string TypeConverter::int64ToString(__int64* item)
{
	stringstream ss;
	if(ss << *item)
	{
		return ss.str();
	}
	else
	{
		return "Error";
	}
}

const string TypeConverter::int64ToConstString(__int64 *item)
{
	return this->int64ToString(item);
}

float TypeConverter::int64ToFloat(__int64 *item)
{
	return (float)*item;
}

const float TypeConverter::int64ToConstFloat(__int64 *item)
{
	return (float)*item;
}

//~INT64TO

//DOUBLE
char* TypeConverter::doubleToChar(double *item)
{
	sprintf_s(char_buffer,CharOfIntBufferSize,"%20.4f",*item);
	return char_buffer;
}

char* TypeConverter::doubleToChar(double *item, int vorkomma, int nachkomma, bool nachkomma_kuerzen)
{
	if(nachkomma_kuerzen)
	{
		while((vorkomma + nachkomma) > 21)
		{
			nachkomma--;
		}
	}
	else
	{
		while((vorkomma + nachkomma) > 21)
		{
			vorkomma--;
		}
	}
	stringstream vor;
	stringstream nach;
	string plattern;
	if((vor << vorkomma) && (nach << nachkomma))
	{
		plattern = '%' + vor.str()+'.'+nach.str()+'f';
	}
	sprintf_s(char_buffer,CharOfIntBufferSize,plattern.c_str(),*item);
	return char_buffer;
}

const char* TypeConverter::doubleToConstChar(double *item)
{
	return doubleToChar(item);
}

const char* TypeConverter::doubleToConstChar(double *item, int vorkomma, int nachkomma, bool nachkomm_kuerzen)
{
	return doubleToChar(item,vorkomma,nachkomma,nachkomm_kuerzen);
}

wchar_t* TypeConverter::doubleToWChar(double* item)
{
	
	_gcvt_s(this->wchar_c_buffer,this->Wchar_c_buffer_Size,*item,30);
	size_t size;
	mbstowcs_s(&size,wchar_t_buffer,30,this->wchar_c_buffer,32);
    return wchar_t_buffer;
}

const wchar_t* TypeConverter::doubleToConstWChar(double *item)
{
	return this->doubleToWChar(item);
}

string TypeConverter::doubleToString(double* item,int nachkomma)
{
	stringstream ss;
	ss.setf(ios_base::fixed,ios_base::floatfield);
	ss.precision(nachkomma);
	if(ss << *item)
	{
		return ss.str();
	}
	else
	{
		return "Error";
	}
}

const string TypeConverter::doubleToConstString(double *item, int nachkomma)
{
	return this->doubleToString(item,nachkomma);
}

int TypeConverter::doubleToInt(double* item)
{
	return (int)*item;
}

const int TypeConverter::doubleToConstInt(double *item)
{
	return (int)*item;
}

__int64 TypeConverter::doubleToInt64(double *item)
{
	return (__int64)*item;
}

const __int64 TypeConverter::doubleToConstInt64(double *item)
{
	return (__int64)*item;
}

float TypeConverter::doubleToFloat(double *item)
{
	return (float)*item;
}

const float TypeConverter::doubleToConstFloat(double *item)
{
	return (float)*item;
}
//~DOUBLE
//FLOAT
char* TypeConverter::floatToChar(float *item)
{
	_gcvt_s(char_buffer,32,*item,30);
	return this->char_buffer;
}

const char* TypeConverter::flaotToConstChar(float *item)
{
	return this->floatToChar(item);
}

wchar_t* TypeConverter::floatToWChar(float* item)
{
    _gcvt_s(this->wchar_c_buffer,this->Wchar_c_buffer_Size,*item,30);
	size_t size;
	mbstowcs_s(&size,wchar_t_buffer,30,this->wchar_c_buffer,32);
	return  wchar_t_buffer;
}

const wchar_t* TypeConverter::floatToConstWChar(float *item)
{
	return floatToWChar(item);
}

string TypeConverter::floatToString(float *item, int nachkomma)
{
	stringstream ss;
	ss.setf(ios_base::fixed,ios_base::floatfield);
	ss.precision(nachkomma);
	if(ss << *item)
	{
		return ss.str();
	}
	else
	{
		return "Error";
	}
}

const string TypeConverter::floatToConstString(float* item,int nachkomma)
{
	return floatToString(item,nachkomma);
}

int TypeConverter::floatToInt(float* item)
{
	return (int)*item;
}

const int TypeConverter::floatToConstInt(float* item)
{
	return (int)*item;
}

__int64 TypeConverter::floatToInt64(float* item)
{
	return (__int64)*item;
}

const __int64 TypeConverter::floatToConstInt64(float* item)
{
	return (__int64)*item;
}

double TypeConverter::floatToDouble(float* item)
{
	return (double)*item;
}

const double TypeConverter::floatToConstDouble(float* item)
{
	return (double)*item;
}
//~FLOAT
//CHAR
int TypeConverter::charToInt(char* item)
{
 return atoi(item);
}

const int TypeConverter::charToConstInt(char *item)
{
	return atoi(item);
}

__int64 TypeConverter::charToInt64(char* item)
{
	return _atoi64(item);
}

const __int64 TypeConverter::charToConstInt64(char* item)
{
	return _atoi64(item);
}
float TypeConverter::charToFloat(char *item)
{
	
	return (float)atof(item);
}

const float TypeConverter::charToConstFloat(char *item)
{
	return (float)atof(item);
}

double TypeConverter::charToDouble(char* item)
{
	return atof(item);
}

const double TypeConverter::charToConstDouble(char* item)
{
	return atof(item);
}

string TypeConverter::charToString(char *item)
{
	stringstream ss;
	if(ss << item)
	{
		return ss.str();
	}
	else
	{
		return "Error";
	}
}

const string TypeConverter::charToConstString(char *item)
{
	return charToString(item);
}

wchar_t* TypeConverter::charToWChar(char *item)
{
	size_t size;
	size_t origsize = strlen(item)+1;
	mbstowcs_s(&size,this->wchar_t_buffer,origsize,item,_TRUNCATE);
	return this->wchar_t_buffer;
}

const wchar_t* TypeConverter::charToConstWChar(char *item)
{
	return charToWChar(item);
}
//~CHAR
//WCHAR_T

int TypeConverter::wchar_tToInt(wchar_t* item)
{
	return _wtoi(item);
}

const int TypeConverter::wchar_tToConstInt(wchar_t* item)
{
	return _wtoi(item);
}

__int64 TypeConverter::wchar_tToInt64(wchar_t *item)
{
	return _wtoi64(item);
}

const __int64 TypeConverter::wchar_tToConstInt64(wchar_t* item)
{
	return _wtoi64(item);
}

float TypeConverter::wchar_tToFloat(wchar_t* item)
{
	return (float)_wtof(item);
}

const float TypeConverter::wchar_tToConstFloat(wchar_t* item)
{
	return (float)_wtof(item);
}

double TypeConverter::wchar_tToDouble(wchar_t* item)
{
	return _wtof(item);
}

string TypeConverter::wchar_tToString(wchar_t* item)
{
	string result;
	result = (char)(item)[0];
	int i = 1;
	int max = wcslen(item);
while((item) && (i<max))
	{	
		result +=(char)(item)[i];
		i++;
	}
result+='\0';
return result;
}
//after this char_buffer has the length of(item)
char* TypeConverter::wchar_tToChar(wchar_t *item)
{
	int max = wcslen(item);
	int i = 0;
	while(i<max)
	{
		this->char_buffer[i] = (char)(item)[i];
		i++;
	}
	this->char_buffer[i] = '\0';
	return this->char_buffer;
}

const char* TypeConverter::wchar_tToConstChar(wchar_t* item)
{
	return wchar_tToChar(item);
}

const string TypeConverter::wchar_tToConstString(wchar_t* item)
{
	return wchar_tToString(item);
}
const double TypeConverter::wchar_tToConstDouble(wchar_t* item)
{
	return _wtof(item);
}

//~WCHAR_T
//STRING

int TypeConverter::stringToInt(string* item)
{
	return atoi(&(*item)[0]);
}

const int TypeConverter::stringToConstInt(string *item)
{
	return atoi(&(*item)[0]);
}

__int64 TypeConverter::stringToInt64(string* item)
{
	return _atoi64(&(*item)[0]);
}

const __int64 TypeConverter::stringToConstInt64(string *item)
{
	return _atoi64(&(*item)[0]);
}

float TypeConverter::stringToFloat(string* item)
{
	return (float)atof(&(*item)[0]);
}

const float TypeConverter::stringToConstFloat(string* item)
{
	return (float)atof(&(*item)[0]);
}

double TypeConverter::stringToDouble(string* item)
{
	return atof(&(*item)[0]);
}

const double TypeConverter::stringToConstDouble(string* item)
{
	return atof(&(*item)[0]);
}

char* TypeConverter::stringToChar(string* item)
{
	return &(*item)[0];
}

const char* TypeConverter::stringToConstChar(string* item)
{
	return &(*item)[0];
}

wchar_t* TypeConverter::stringToWchar_T(string *item)
{
	int i = 0;
	while((*item)[i] != 0)
	{
		this->wchar_t_buffer[i] = (*item)[i];
		i++;
	}
	this->wchar_t_buffer[i] = '\0';
	return this->wchar_t_buffer;
}

const wchar_t* TypeConverter::stringToConstWchar_T(string* item)
{
	
	return stringToWchar_T(item);
}
//~STRING
void TypeConverter::ShowTest()
{
	



this->set_intToCharSize(11);
cout.setf( ios_base::fixed, ios_base::floatfield );
cout.precision(10);
cout<<"#######################################"<<endl;
cout<<"###############CONVERTER###############"<<endl;
cout<<"#######################################"<<endl;
cout<<"Typ INT: "<<int1<<endl;
cout<<"Typ INT64: "<<int64<<endl;
cout<<"Typ INT64: "<<int642<<endl;
cout<<"Typ DOUBLE: "<<doub<<endl;
cout<<"Typ DOUBLE: "<<doub2<<endl;
cout<<"Typ FLOAT: "<<flo<<endl;
cout<<"Typ CHAR: "<<satz<<endl;
cout<<"Typ CHAR: "<<ch<<endl;
wcout<<"Typ WCHAR_T: "<<wchar<<endl;
wcout<<"Typ WCHAR_T: "<<wsatz<<endl;
cout<<"Typ STRING: "<<str<<endl;
cout<<"Typ STRING: "<<str2<<endl;

cout<<"<---------------INTEGER--------------->"<<endl;
cout<<"intToChar: "<<this->intToChar(&int1)<<endl;
cout<<"intToConstChar: "<<this->intToConstChar(&a)<<endl;
wcout<<"intToWChar: "<<this->intToWChar(&int1)<<endl;
wcout<<"intToConstWChar: "<<this->intToConstWChar(&int1)<<endl;
cout<<"intToString: "<<this->intToString(&int1)<<endl;
cout<<"intToConstString: "<<this->intToConstString(&int1)<<endl;
cout<<"intToDouble: "<<this->intToDouble(&int1)<<endl;
cout<<"intToConstDouble: "<<this->intToConstDouble(&int1)<<endl;
cout<<"intToFloat: "<<this->intToFloat(&int1)<<endl;
cout<<"intToConstFloat: "<<this->intToConstFloat(&int1)<<endl;
cout<<"intToInt64: "<<this->intToInt64(&int1)<<endl;
cout<<"intToConstInt64: "<<this->intToConstInt64(&int1)<<endl;
cout<<"<----------INTEGER-ENDE--------------->"<<endl;
cout<<"<--------------INT64------------------>"<<endl;
cout<<"int64ToChar: "<<this->int64ToChar(&int64)<<endl;
cout<<"int64ToConstChar: "<<this->int64ToConstChar(&int64)<<endl;
wcout<<"int64ToWChar: "<<this->int64ToWChar(&int64)<<endl;
wcout<<"int64ToConstChar: "<<this->int64ToConstWChar(&int64)<<endl;
cout<<"int64ToDouble: "<<this->int64ToDouble(&int64)<<endl;
cout<<"int64ToConstDouble: "<<this->int64ToConstDouble(&int64)<<endl;
cout<<"int64ToInt: "<<this->int64ToInt(&int64)<<endl;
cout<<"int64ToConstInt: "<<this->int64ToConstInt(&int64)<<endl;
cout<<"int64ToString: "<<this->int64ToString(&int64)<<endl;
cout<<"int64ToConstString: "<<this->int64ToConstString(&int64)<<endl;
cout<<"int64ToFloat: "<<this->int64ToFloat(&int64)<<endl;
cout<<"int64ToConstFloat: "<<this->int64ToConstFloat(&int64)<<endl;
cout<<"<-------------INT 64 Ende------------->"<<endl;
cout<<"<--------------DOUBLE----------------->"<<endl;
cout<<"doubleToChar: "<<this->doubleToChar(&doub)<<endl;
cout<<"doubleToChat(12345.678901,5,10,true): "<<this->doubleToChar(&doub2,5,10,true)<<endl;
cout<<"doubleToConstChar: "<<this->doubleToConstChar(&doub)<<endl;
cout<<"doubleToConstChar(12345.678901,5,10,true): "<<this->doubleToConstChar(&doub2,5,10,true)<<endl;
wcout<<"doubleToWChar: "<<this->doubleToWChar(&doub2)<<endl;
wcout<<"doubleToConstWchar: "<<this->doubleToConstWChar(&doub)<<endl;
cout<<"doubleToString(nachkomma 1): "<<this->doubleToString(&doub2,1)<<endl;
cout<<"doubleToString(nachkomma 10): "<<this->doubleToString(&doub2,10)<<endl;
cout<<"doubleToConstString(nachkomma 1): "<<this->doubleToConstString(&doub,1)<<endl;
cout<<"doubleToConstString(nachkomma 10): "<<this->doubleToConstString(&doub,10)<<endl;
cout<<"doubleToInt: "<<this->doubleToInt(&doub2)<<endl;
cout<<"doubleToConstInt: "<<this->doubleToConstInt(&doub2)<<endl;
cout<<"doubleToInt64: "<<this->doubleToInt64(&doub2)<<endl;
cout<<"doubleToConstInt64: "<<this->doubleToConstInt64(&doub2)<<endl;
cout<<"doubleToFloat: "<<this->doubleToFloat(&doub2)<<endl;
cout<<"doubleToConstFloat: "<<this->doubleToConstFloat(&doub2)<<endl;
cout<<"<----------------DOUBLE ENDE----------->"<<endl;
cout<<"<-----------------FLOAT---------------->"<<endl;
cout<<"floatToChar: "<<this->floatToChar(&flo)<<endl;
cout<<"floatToConstChar: "<<this->flaotToConstChar(&flo)<<endl;
wcout<<"floatToWChar: "<<this->floatToWChar(&flo)<<endl;
wcout<<"floatToConstWChar: "<<this->floatToConstWChar(&flo)<<endl;
cout<<"floatToString(nachkomma 1): "<<this->floatToString(&flo,1)<<endl;
cout<<"floatToString(nachkomma 10): "<<this->floatToString(&flo,10)<<endl;
cout<<"floatToConstString(nachkomma 1): "<<this->floatToConstString(&flo,1)<<endl;
cout<<"floatToConstString(nachkomma 10): "<<this->floatToConstString(&flo,10)<<endl;
cout<<"floatToInt: "<<this->floatToInt(&flo)<<endl;
cout<<"floatToConstInt: "<<this->floatToConstInt(&flo)<<endl;
cout<<"floatToInt64: "<<this->floatToInt64(&flo)<<endl;
cout<<"floatToConstInt64: "<<this->floatToConstInt64(&flo)<<endl;
cout<<"floatToDouble: "<<this->floatToDouble(&flo)<<endl;
cout<<"floatToConstDouble: "<<this->floatToConstDouble(&flo)<<endl;
cout<<"<-----------------FLOAT ENDE ----------->"<<endl;
cout<<"<-------------------CHAR---------------->"<<endl;
cout<<"charToInt: "<<this->charToInt(ch)<<endl;
cout<<"charToConstInt: "<<this->charToConstInt(ch)<<endl;
cout<<"charToInt64: "<<this->charToInt64(ch)<<endl;
cout<<"charToConstInt64: "<<this->charToConstInt64(ch)<<endl;
cout<<"charToFloat: "<<this->charToFloat(ch)<<endl;
cout<<"charToConstFloat: "<<this->charToConstFloat(ch)<<endl;
cout<<"charToDouble: "<<this->charToDouble(ch)<<endl;
cout<<"charToConstDouble: "<<this->charToConstDouble(ch)<<endl;
cout<<"charToString: "<<this->charToString(satz)<<endl;
cout<<"charToConstString: "<<this->charToConstString(satz)<<endl;
wcout<<"charToWChar: "<<this->charToWChar(satz)<<endl;
wcout<<"charToConstWChar: "<<this->charToConstWChar(satz)<<endl;
cout<<"<-----------------CHAR ENDE------------->"<<endl;
cout<<"<------------------WCHAR_T-------------->"<<endl;
cout<<"wchar_tToInt: "<<this->wchar_tToInt(wchar)<<endl;
cout<<"wchar_tToConstInt: "<<this->wchar_tToConstInt(wchar)<<endl;
cout<<"wchar_tToInt64: "<<this->wchar_tToInt64(wchar)<<endl;
cout<<"wchar_tToConstInt64: "<<this->wchar_tToConstInt64(wchar)<<endl;
cout<<"wchar_tToFloat: "<<this->wchar_tToFloat(wchar)<<endl;
cout<<"wchar_tToConstFloat: "<<this->wchar_tToConstFloat(wchar)<<endl;
cout<<"wchar_tToDouble: "<<this->wchar_tToDouble(wchar)<<endl;
cout<<"wchar_tToConstDouble: "<<this->wchar_tToConstDouble(wchar)<<endl;
cout<<"wchar_tToString: "<<this->wchar_tToString(wsatz)<<endl;
cout<<"wchar_tToConstString: "<<this->wchar_tToConstString(wsatz)<<endl;
cout<<"wchar_tToChar: "<<this->wchar_tToChar(wsatz)<<endl;
cout<<"wchar_tToConstChar: "<<this->wchar_tToConstChar(wsatz)<<endl;
cout<<"<-----------------WCHAR_T ENDE---------->"<<endl;
cout<<"<------------------STRING--------------->"<<endl;
cout<<"stringToInt: "<<this->stringToInt(&str)<<endl;
cout<<"stringToConstInt: "<<this->stringToConstInt(&str)<<endl;
cout<<"stringToInt64: "<<this->stringToInt64(&str)<<endl;
cout<<"stringToConstInt64: "<<this->stringToConstInt64(&str)<<endl;
cout<<"stringToFloat: "<<this->stringToFloat(&str)<<endl;
cout<<"stringToConstFloat: "<<this->stringToConstFloat(&str)<<endl;
cout<<"stringToDouble: "<<this->stringToDouble(&str)<<endl;
cout<<"stringToConstDouble: "<<this->stringToConstDouble(&str)<<endl;
cout<<"stringToChar: "<<this->stringToChar(&str2)<<endl;
cout<<"stringToConstChar: "<<this->stringToConstChar(&str2)<<endl;
wcout<<"stringToWchar_T: "<<this->stringToWchar_T(&str2)<<endl;
wcout<<"stringToConstWchar_T: "<<this->stringToConstWchar_T(&str2)<<endl;
cout<<"<----------------STRING ENDE------------>"<<endl;

}
void TypeConverter::set_intToCharSize(int size)
	{
	intToCharSize = size;
	}

int TypeConverter::get_intToCharSize()
	{
	return intToCharSize;
	}



"Whoops..."
jeetee
Posts: 15
Joined: Tue Oct 13, 2009 3:09 pm
Contact:

Post by jeetee »

That could be so much shorter by using a template I think.
I must have a conversionfunction lying around somewhere at home for some of the common STL-types, will post later today.

In any case, static casting is always the better choice imo, but sometimes dynamic casting with stringstreams can be more convenient (user input anyone?).

[EDIT]Don't expect too much of my code... It is dated to before I got a 'real' grasp on const-correctness.[/EDIT]
jeetee
Posts: 15
Joined: Tue Oct 13, 2009 3:09 pm
Contact:

Post by jeetee »

And then I had to go and get ill.. anyway, here's my small convert-function (back then I still coded in Dutch)
convert.hpp

Code: Select all

#ifndef CONVERSIE_HPP
#define CONVERSIE_HPP

#include <iostream>
#include <sstream>
using namespace std;

template <class doelType, class inputType>

doelType convert(const inputType & t)
{
    stringstream s;
    s << t;
    doelType resultaat;
    s >> resultaat;
    return resultaat;
}

#endif  //CONVERSIE_HPP
Post Reply