Umwandeln | Hex --> Dezimal --> Char
-
char hello[1024];
cin >> hello;
if( hello[0]='0' ) {
hello[0] = 48;
}
else if( hello[0]='1' ) {
hello[0] = 49;
}
else if( hello[0]='2' ) {
hello[0] = 50;
}
else if( hello[0]='3' ) {
hello[0] = 51;
}
else if( hello[0]='4' ) {
hello[0] = 52;
}
else if( hello[0]='5' ) {
hello[0] = 53;
}
else if( hello[0]='6' ) {
hello[0] = 54;
}
else if( hello[0]='7' ) {
hello[0] = 55;
}
else if( hello[0]='8' ) {
hello[0] = 56;
}
else if( hello[0]='9' ) {
hello[0] = 57;
}
else if( hello[0]='a' || 'A' ) {
hello[0] = 65;
}
else if( hello[0]='b' || 'B') {
hello[0] = 66;
}
else if( hello[0]='c' || 'C') {
hello[0] = 67;
}
else if( hello[0]='d' || 'D') {
hello[0] = 68;
}
else if( hello[0]='e' || 'E') {
hello[0] = 69;
}
else if( hello[0]='f' || 'F') {
hello[0] = 70;
}
else if( hello[0]='g' || 'G') {
hello[0] = 71;
}
else if( hello[0]='h' || 'H') {
hello[0] = 72;
}
else if( hello[0]='i' || 'I') {
hello[0] = 73;
}
else if( hello[0]='j' || 'J') {
hello[0] = 74;
}
else if( hello[0]='k' || 'K') {
hello[0] = 75;
}
else if( hello[0]='l' || 'L') {
hello[0] = 76;
}
else if( hello[0]='m' || 'M') {
hello[0] = 77;
}
else if( hello[0]='n' || 'N') {
hello[0] = 78;
}
else if( hello[0]='o' || 'O') {
hello[0] = 79;
}
else if( hello[0]='p' || 'P') {
hello[0] = 80;
}
else if( hello[0]='q' || 'Q') {
hello[0] = 81;
}
else if( hello[0]='r' || 'R') {
hello[0] = 82;
}
else if( hello[0]='s' || 'S') {
hello[0] = 83;
}
else if( hello[0]='t' || 'T') {
hello[0] = 84;
}
else if( hello[0]='u' || 'U') {
hello[0] = 85;
}
else if( hello[0]='v' || 'V') {
hello[0] = 86;
}
else if( hello[0]='w' || 'W') {
hello[0] = 87;
}
else if( hello[0]='x' || 'X') {
hello[0] = 88;
}
else if( hello[0]='y' || 'Y') {
hello[0] = 89;
}
else if( hello[0]='z' || 'Z') {
hello[0] = 90;
}So dabei hab ich folgende probleme:
Erstens kriegt hello[0] irgendwie JEDESMAL die erste umgewandelte eingabe also in diesem beispiel ist hello[0} = 48 auch wenn ich nicht einmal die 0 eingeb.
is genauso wenn ich in if noch keine umwandlung hab und danach erst oder ist auch mit jedem anderen buchstaben so :>
Wenn ihr wisst wie ich das gefixt bekomme wär ich sehr dankbar wenn ihr mir meinen Fehelr sagen könntet.Zweite problem:
Wie wandel ich nun Space , Tab, Backspace etc um ?
für space zb würde DAS -->
if( hello[0] =' ' ) {
hello[0] = 32;
}
nicht klappen.
Wenns ich in den ' ' white space oder Space oder so versuche gehts auch nicht.
Also wäre auch sehr dankbar wenn mir einer von euch sagen könnte wie ich das mit den Tasten wie Tab backspace Space ESC home usw usw usw mache.
Danke im vorraus.mfg
klompt
-
klompt schrieb:
char hello[1024]; cin >> hello; // so wird das nicht funktionieren, nimm getline if( hello[0]='0' ) { hello[0] = 48; // mach doch switch(hello[0]) {case...} } else if( hello[0]='1' ) { hello[0] = 49; } else if( hello[0]='2' ) { hello[0] = 50; } else if( hello[0]='3' ) { hello[0] = 51; } else if( hello[0]='4' ) { hello[0] = 52; } else if( hello[0]='5' ) { hello[0] = 53; } else if( hello[0]='6' ) { hello[0] = 54; } else if( hello[0]='7' ) { hello[0] = 55; } else if( hello[0]='8' ) { hello[0] = 56; } else if( hello[0]='9' ) { hello[0] = 57; } else if( hello[0]='a' || 'A' ) { hello[0] = 65; } else if( hello[0]='b' || 'B') { hello[0] = 66; } else if( hello[0]='c' || 'C') { hello[0] = 67; } else if( hello[0]='d' || 'D') { hello[0] = 68; } else if( hello[0]='e' || 'E') { hello[0] = 69; } else if( hello[0]='f' || 'F') { hello[0] = 70; } else if( hello[0]='g' || 'G') { hello[0] = 71; } else if( hello[0]='h' || 'H') { hello[0] = 72; } else if( hello[0]='i' || 'I') { hello[0] = 73; } else if( hello[0]='j' || 'J') { hello[0] = 74; } else if( hello[0]='k' || 'K') { hello[0] = 75; } else if( hello[0]='l' || 'L') { hello[0] = 76; } else if( hello[0]='m' || 'M') { hello[0] = 77; } else if( hello[0]='n' || 'N') { hello[0] = 78; } else if( hello[0]='o' || 'O') { hello[0] = 79; } else if( hello[0]='p' || 'P') { hello[0] = 80; } else if( hello[0]='q' || 'Q') { hello[0] = 81; } else if( hello[0]='r' || 'R') { hello[0] = 82; } else if( hello[0]='s' || 'S') { hello[0] = 83; } else if( hello[0]='t' || 'T') { hello[0] = 84; } else if( hello[0]='u' || 'U') { hello[0] = 85; } else if( hello[0]='v' || 'V') { hello[0] = 86; } else if( hello[0]='w' || 'W') { hello[0] = 87; } else if( hello[0]='x' || 'X') { hello[0] = 88; } else if( hello[0]='y' || 'Y') { hello[0] = 89; } else if( hello[0]='z' || 'Z') { hello[0] = 90; }
und benutz bitte cpp tags
ich würde das ganze eher per schleife durchgehen lassen
aber ich verstehe dein problem nicht so genau
-
#include <iostream> int main( ) { if( 'A' == 65 ) { std::cout << "Deine \"Umwandlung\" ist für die Katz'!" << std::endl; } }
guckst du: ASCII-Tabelle
cheers, Swordfish
-
Swordfish schrieb:
#include <iostream> int main( ) { if( 'A' == 65 ) { std::cout << "Deine \"Umwandlung\" ist für die Katz'!" << std::endl; } }
guckst du: ASCII-Tabelle
cheers, Swordfish
Du musst es ja wissen, ich will das in verbindung mit keybd_event benutzen ;P
-
Switch hab ich versucht klappt leider nicht, und naja, mit cin.getline(hello,1024)
bleibt das problem trotzdem das hello[0] immer die zahl der ersten umwandlung erhält auch wenn ich diese taste nicht eingebe
-
klompt schrieb:
Du musst es ja wissen, ich will das in verbindung mit keybd_event benutzen ;P
klompt schrieb:
if( hello[0]='0' ) { hello[0] = 48; }
... ist und bleibt völlig sinnfrei - egal in welchem Zusammenhang
klompt schrieb:
Switch hab ich versucht klappt leider nicht,
"Klappt leider nicht" ist keine Fehlerbeschreibung...
klompt schrieb:
und naja, mit cin.getline(hello,1024) bleibt das problem trotzdem das hello[0] immer die zahl der ersten umwandlung erhält auch wenn ich diese taste nicht eingebe
Dieses Dein Problem kann ich nicht nachvollziehen, da
#include <iostream> int main( ) { char input[ 1024 ]; std::cin.getline( input, sizeof( input ) ); for( char *p = input; *p; ++p ) { std::cout << "'" << *p << "' == 0x" << std::hex << static_cast< int >( *p ); std::cout << " == " << std::dec << static_cast< int >( *p ) << std::endl; } }
bei mir mit allen möglichen Eingaben immer auf
input[ 0 ]
dem ersten eingegebenem Zeichen entspricht.Vielleicht kann man Dir helfen, wenn Du beschreibst, was Du erreichen willst - zB der erwünschte Inhalt des Arrays vor der Konvertierung und danach.
cheers, Swordfish
-
*lol* jetzt seh' ich's erst. Deine Bedingungen in den
if
-Statements sind Zuweisungen (=
) und keine Vergleiche (==
)if( hello[ 0 ] = '0' ) { hello[ 0 ] = 48; }
hello[ 0 ] = '0'
ergibt48
. Das dann nachbool
gekastet auf jeden Fall immertrue
, womit das Mysterium gelöst wäre, warumhello[ 0 ]
bei dir immer48
bekommt.Weiters willst Du wahrscheinlich nicht
//... if( hello[ 0 ] = 'a' || 'A' ) { hello[ 0 ] = 65; }
sondern
if( hello[ 0 ] == 'a' || hello[ 0 ] == 'A' ) { hello[ 0 ] = 65; }
Einfacher wäre aber
hello[ 0 ] = std::toupper( hello[ 0 ] );
cheers, Swordfish
-
Danke swordfish ich werds mal testen
hätt ich selbst draufkommen können lol *schäm* ^^naja bleibt noch folgendes problem:
Wie wandel ich nun Space , Tab, Backspace etc um ?
für space zb würde DAS -->
if( hello[0] =' ' ) {
hello[0] = 32;
}
nicht klappen.
Wenns ich in den ' ' white space oder Space oder so versuche gehts auch nicht.
Also wäre auch sehr dankbar wenn mir einer von euch sagen könnte wie ich das mit den Tasten wie Tab backspace Space ESC home usw usw usw mache.
Danke im v******.mfg
klompt
-
Warum ds Ganze eigentlich nicht mit switch-case anweisung anstelle der vielen If's ?
-
@Quellcode: Solange ich den Sinn der Umwandlungen des OPs nicht kenne, halt' ich mich lieber mit Ratschlägen bzgl. eleganz mal zurück
@OP:
klompt schrieb:
Wie wandel ich nun Space , Tab, Backspace etc um ?
für space zb würde DAS -->if( hello[ 0 ] = ' ' ) { hello[ 0 ] = 32; }
nicht klappen.
1. cpp-Tags sind was schönes...
2. das Statement imif
ist schon wieder (noch immer?) eine Zuweisung, kein Vergleich.
3. Wär's ein Vergleich wär's auch sinnfrei, da' ' == 32
!!!111elfDu hast uns immer noch nicht verraten, was Du eigentlich genau wie Umwandeln willst. ich Rate mal in's Blaue:
if( hello[ 0 ] == 10 /* backspace */ || hello[ 0 ] == 11 /* tab */ || hello[ 0 ] == 13 /* vertical tab */ || hello[ 0 ] == ' ' /* space (== 32) */ || hello[ 0 ] == 27 /* esc */ ) { hello[ 0 ] = 32; // wobei die Zuweisung im falle hello[ 0 ] == ' ' sinnfrei ist. }
Mit Home (Pos1) wirst Du dir schwer tun, da der Keyboard Controller da einen Scancode generiert, der sich nicht in ASCII wiederfindet. Zeichen wie ESC solltest Du IMHO von
getline( )
aber garnicht erst bekommen.Vielleicht magst Du endlich bescheiben, was Eingabe und was Ausgabe sein soll?
cheers, Swordfish