fehler ! " text in hex und dezimal umwandeln "

DJIN

Grünschnabel
hi
ich habe ein problem !
wie im thread titel schon steht möchte ich einfach nur etwas in die konsole eingeben und das programm soll es in einen hex code und in einen dezimal code umwandeln !

hier mein code



HTML:
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string input;
    
    
    if (getline(cin,input));
    
    {
                            cout<<"dezimalcode: "<<(int)input<<endl<<endl;;    //<-- hier ist der fehler
                            cout<<"Hexcode: "<<hex<<(int)input<<endl;
                            system("PAUSE");
                            return 0;
                            }
                            
                            
                            
                            system("PAUSE");
                            return 0;
                            
                            }

das programm ist noch nicht fertig ich will das noch weiter progen ! aber ich komme mit
diesem fehler nicht klar !


kann jemand helfen ?

danke !
 
Zuletzt bearbeitet:
Hallo,
mit einem Typecast kannst du aus dem String keinen Integer machen. Dazu musst du den String konvertieren, z.B. mit Hilfe eines Stringstream:
C++:
#include <sstream>

// ...

int nInput;
stringstream sstr(input);
sstr >> nInput;

cout << "dezimalcode: " << nInput << endl << endl;
cout << "Hexcode    : " << hex << nInput << endl;
Alternativ kannst du natürlich die Eingabe auch gleich in eine int-Variable einlesen:
C++:
cout << "Zahl eingeben:";
int nInput;
cin >> nInput;
Gruß
MCoder
 
@ über mir big thx
habe es jetzt so hin bekommen !


HTML:
 #include <iostream.h>
 
 using namespace std;

 int main()
 {
 char Eingabe;
 int zahl;

 cin>>(Eingabe);
 zahl = (int)Eingabe;
 cout<<(zahl);

 system("PAUSE");
 return 0;
 }


aber ich will mehr als nur einen buchstaben eingeben und umgewandelt bekommen ! wie geht das ?
 
Code:
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;

enum eOpt
{
	EOPT_CONV_DEC = 0,
	EOPT_CONV_HEX,
};

void printHelp( char* sProg );

int main( int argc, char *argv[] )
{
	eOpt	eMode	= EOPT_CONV_DEC; /* setting decimal to default */
	size_t	curSize	= 0;

	/* used to run the program without arguments? */
	if( argc < 3 ) 
	{
		/* then print the help, and exit */
		printHelp( *argv );
		return EXIT_SUCCESS;
	}
	else
	{
		/* check options */
		if( strcmp( *++argv, "-hex" ) == 0 )
		{
			eMode = EOPT_CONV_HEX;
		}
		/* else: default setting */
	}

	/* increment each run */
	while( *++argv )
	{
		/* get var.-size */
		curSize = strlen( *argv );
		
		/* while iCnt is lower then cursize.. increment */
		for( size_t iCnt=0; iCnt < curSize; iCnt++ )
		{
			/* check output mode */
			if( eMode == EOPT_CONV_HEX )
			{
				cout << "\\x" << hex << (int)*(*argv+iCnt) << " ";
			}
			else
			{
				cout << dec << (int)*(*argv+iCnt) << " ";
			}
		}

		cout << endl;
	}

	return EXIT_SUCCESS;
}

void printHelp( char* sProg )
{
	cout    << "usage: " << sProg << " <option> <string>" << endl << endl
			<< "Options:" << endl
		 	<< "-dec\t\tconvert to decimal" << endl
			<< "-hex\t\tconvert to hexadecimal" << endl;

	return;
}

Ich habe die Kommentare versehentlich in Englisch geschrieben ( mache ich immer.. ), aber ich denke du kannst Englisch ( immerhin ist es das A und O ).

Wenn noch fragen da sind, meld dich ;).
 
@MCoder

Kenne mich mit stringstreams garnicht aus aber kann es sein das du einen Int in einen String wandelst ? Wenn das der Fall ist, wollte er es andersherum.

Alternativ könnte man noch atoi() benutzen.
C++:
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string input;
    if (getline(cin,input))
    {
      cout << atoi(input.c_str());
    }
	return 0;
}

mfg ;-)
 
@Online-Skater
Nein, das ist schon die richtige Richtung: der String "input" wird nach der int-Variablen "nInput" konvertiert.
 
@ über mir
also entweder hier ist ein fehler im code oder ich habe ihn falsch zusammen gebaut ( was ich eher denke ! )


HTML:
      #include <sstream>


using namespace std;

    int main()
    {

       

      int nInput;

      stringstream sstr(input);

      sstr >> nInput;

       

      cout << "dezimalcode: " << nInput << endl << endl;

      cout << "Hexcode    : " << hex << nInput << endl; 
      
      
      
      system("PAUSE");
      return 0;
}
 
Den Teil für die Eingabe brauchst du natürlich auch noch. Ich hatte keinen vollständigen Code, sondern nur den dein Problem betreffenden Abschnitt gepostet.
C++:
string input;

if( getline(cin,input) )
{
    // Konvertierung und Ausgabe
}
 
ja ich habe mich schon gewundert wieso der code so mager ist :D


jo es klappt !

aber er zeigt immer den gleichen hex code und den dezimal code an egal was für buchstaben ich eingebe !


HTML:
      #include <iostream>
      #include <string>
      #include <sstream>


using namespace std;

    int main()
    {

       

      string input;

       

      if( getline(cin,input) )
      {
          int nInput;

      stringstream sstr(input);

      sstr >> nInput;

       

      cout << "dezimalcode: " << nInput << endl << endl;

      cout << "Hexcode    : " << hex << nInput << endl; 
      }
      
      
      system("PAUSE");
      return 0;
}
 
Zuletzt bearbeitet:
Zurück