Konvertierungen (Strings und Zahlen)



  • Da ich immer wieder Probleme mit Konvertierungen hatte und mir auch im Forum immer weitergeholfen wurde, habe ich mal ein paar Methoden zur Konvertierung aufgelistet. Vielleicht kann jemand das ein oder andere gebrauchen. Hab gerade erst angefangen zu sammeln, so dass das Ganze noch größer werden wird.

    // File: convert.h
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <stdexcept>
    
    string ConvertDoubleToString(double x)
    {
    	ostringstream o;
    	(o << x)
    	return o.str();
    }
    
    CString ConvertStringToCString(string s) {
    	CString cs = s.c_str();
    
    	return cs;
    }
    
    static string ConvertLongToString(long longNum)
    {
    	string result;
    	char temp;
    
    	temp = longNum & 0xF;
    	result += temp;
    
    	return result;
    }
    
    int ConvertCStringToInt(CString s) {
      int number = atoi(s);
    
      return number;
    }
    
    CString ConvertIntToCString(int number) {
    	ostringstream str;
    	str << number; 
    	string zahlAlsString(str.str());
    	CString cs = zahlAlsString.c_str();
    
      return cs;
    }
    
    CString ConvertIntToCString(int number, int numberOfMinimumDigits) {
    	ostringstream str;
    	str << number; 
    	string zahlAlsString(str.str());
    	while(zahlAlsString.length() < numberOfMinimumDigits) 
    		zahlAlsString = "0" + zahlAlsString;
    	CString cs = zahlAlsString.c_str();
    
      return cs;
    }
    
    CString ConvertLongToCString(long number) {
    	ostringstream str;
    	str << number; 
    	string zahlAlsString(str.str());
    	CString cs = zahlAlsString.c_str();
    
      return cs;
    }
    
    // CComboxBox ATL/WTL
    CString ConvertCComboBoxSelectedValueToCString(CComboBox comboBox, int selectedValueNumber) {
      LPTSTR s;
    
      s = new TCHAR[comboBox.GetLBTextLen(selectedValueNumber)+1];
      comboBox.GetLBText(selectedValueNumber, s);
      CString cString(s);
    
      return cString;
    }
    
    // CComboxBox ATL/WTL
    LPTSTR ConvertCComboBoxSelectedValueToLPTSTR(CComboBox comboBox, int selectedValueNumber) {
      LPTSTR s;
    
      s = new TCHAR[comboBox.GetLBTextLen(selectedValueNumber)+1];
      comboBox.GetLBText(selectedValueNumber, s);
    
      return s;
    }
    
    BSTR ConvertCStringToBSTR(CString s) {
      CComBSTR ccBSTR(s);
    	BSTR b = ccBSTR;
    
      return b;
    }
    
    CComBSTR ConvertCStringToCComBSTR(CString s) {
      CComBSTR ccBSTR(s);
    
      return ccBSTR;
    }
    
    string ConvertCStringToString(CString cs) {
    	string s( (const char*)cs );
    
    	return s;
    }
    

    EDIT: Richtig, die Zeile fehlte plötzlich 👍



  • plizer schrieb:

    Da ich immer wieder Probleme mit Konvertierungen hatte und mir auch im Forum immer weitergeholfen wurde, habe ich mal ein paar Methoden zur Konvertierung aufgelistet. Vielleicht kann jemand das ein oder andere gebrauchen. Hab gerade erst angefangen zu sammeln, so dass das Ganze noch größer werden wird.

    // File: convert.h
    
    string ConvertDoubleToString(double x)
    {
    	ostringstream o;
    
    	return o.str();
    }
    }
    

    Fehlt da nicht ein o << x; ?

    (Btw. in der Faq findet sich schon was dazu 🙄)


Anmelden zum Antworten