2016-11-09 3 views
0

Ich versuche ein Programm zu machen, wo der Benutzer eine hexadezimale Zeichenfolge eingibt ("im Format 3ecf, keine 0x und keine Großbuchstaben") Der folgende Code ist mein Versuch zu kopieren, was der Benutzer eingibt (Adresse) und speichern Sie das binäre Äquivalent in binAddress.Hexadezimal String zu Binär String

Wie behebe ich das?
Oder gibt es einen einfacheren Weg?

char address [6];//global 
char binAddress[24]; //global 
scanf("%s", address); //in some other function 

...

void hexToBin(){ 
int i = 0; 
int j = 24; 
int z; 
while(address[i]){ 
    char x[4]; //strcpy(char x, "0000") 
    switch(address[i]){ 
     case '0': strcpy(x, "0000"); break; 
     case '1': strcpy(x, "0001"); break; 
     case '2': strcpy(x, "0010"); break; 
     case '3': strcpy(x, "0011"); break; 
     case '4': strcpy(x, "0100"); break; 
     case '5': strcpy(x, "0101"); break; 
     case '6': strcpy(x, "0110"); break; 
     case '7': strcpy(x, "0111"); break; 
     case '8': strcpy(x, "1000"); break; 
     case '9': strcpy(x, "1001"); break; 
     case 'a': strcpy(x, "1010"); break; 
     case 'b': strcpy(x, "1011"); break; 
     case 'c': strcpy(x, "1100"); break; 
     case 'd': strcpy(x, "1101"); break; 
     case 'e': strcpy(x, "1110"); break; 
     case 'f': strcpy(x, "1111"); break; 
     default: strcpy(x, "0000"); break; 
    } 
    i++; 
    for (z = 3; z > -1; z--){ 
     binAddress[j] = x[z]; 
     j--; 
     printf("%c\n", binAddress[j]); 
    } 
} 
} 
+0

Read [fragen], bieten a [mcve]. Der Code ist unvollständig. – Olaf

+0

'char x [4];' -> 'char x [5];' – BLUEPIXY

Antwort

1

fix wie folgt aus:

#include <stdio.h> 
#include <string.h> 

char address[6+1]; //+1 for NUL 
char binAddress[6*4+1];//+1 for NUL 
void hexToBin(void); 

int main(void){ 
    scanf("%6s", address); 

    hexToBin(); 

    printf("%s\n", binAddress); 
    return 0; 
} 

void hexToBin(){ 
    int i, j; 

    for(j = i = 0; address[i]; ++i, j += 4){ 
     switch(address[i]){ 
     case '0': strcpy(binAddress + j, "0000"); break; 
     case '1': strcpy(binAddress + j, "0001"); break; 
     case '2': strcpy(binAddress + j, "0010"); break; 
     case '3': strcpy(binAddress + j, "0011"); break; 
     case '4': strcpy(binAddress + j, "0100"); break; 
     case '5': strcpy(binAddress + j, "0101"); break; 
     case '6': strcpy(binAddress + j, "0110"); break; 
     case '7': strcpy(binAddress + j, "0111"); break; 
     case '8': strcpy(binAddress + j, "1000"); break; 
     case '9': strcpy(binAddress + j, "1001"); break; 
     case 'a': strcpy(binAddress + j, "1010"); break; 
     case 'b': strcpy(binAddress + j, "1011"); break; 
     case 'c': strcpy(binAddress + j, "1100"); break; 
     case 'd': strcpy(binAddress + j, "1101"); break; 
     case 'e': strcpy(binAddress + j, "1110"); break; 
     case 'f': strcpy(binAddress + j, "1111"); break; 
     default: 
      printf("invalid character %c\n", address[i]); 
      strcpy(binAddress + j, "0000"); break; 
     } 
    } 
} 
0

Sie brauchen nicht hexadezimal Array in char zu speichern. Sie können es über scanf mit Angabe% lesen x
http://www.cplusplus.com/reference/cstdio/scanf/

Dann wandelt es in binäre (C-String-Form) how to print binary number via printf

+0

Muss ich die Adresse in den Typ int ändern? – cowchin12

+0

ja. Aber ich empfehle, unsigned long zu verwenden (um sicherzustellen, dass es 32bit-Integer ist, ist Int plattformabhängige Größe), wenn es in binäre Verwendung & 1u konvertieren, kann das signierte Bit Probleme verursachen. –

Verwandte Themen