Landon
Landon

Reputation: 21

How to convert a hexadecimal value to a binary value in C

How can I fix my code? I have a text file with hexadecimal values. Now I need to convert the hexadecimal value to binary and print it. This is my code so far:

#include <stdio.h>
#include <stdlib.h>
#define MAX 1000

int hex_to_binary(char *argv[])

int main(int argc, char *argv[])
{
    FILE *file;
    file = fopen(argv[1], "r");
    char line[100];

    while(!feof(file)) {
        fgets(line, 100, file);
        hex_to_binary(line);
    }
    fclose(file);

    return 0;
}

int hex_to_binary(char *argv[]) {
    char binaryNumber[MAX], hexaDecimal[MAX];
    long int i = 0;

    scanf(“%s”, argv[1]);

    printf("\nEquivalent binary value: ");
    while(hexaDecimal[i]) {
         switch(hexaDecimal[i]) {
             case '0': printf("0000"); break;
             case '1': printf("0001"); break;
             case '2': printf("0010"); break;
             case '3': printf("0011"); break;
             case '4': printf("0100"); break;
             case '5': printf("0101"); break;
             case '6': printf("0110"); break;
             case '7': printf("0111"); break;
             case '8': printf("1000"); break;
             case '9': printf("1001"); break;
             case 'A': printf("1010"); break;
             case 'B': printf("1011"); break;
             case 'C': printf("1100"); break;
             case 'D': printf("1101"); break;
             case 'E': printf("1110"); break;
             case 'F': printf("1111"); break;
             case 'a': printf("1010"); break;
             case 'b': printf("1011"); break;
             case 'c': printf("1100"); break;
             case 'd': printf("1101"); break;
             case 'e': printf("1110"); break;
             case 'f': printf("1111"); break;
             default:  printf("\nInvalid hexadecimal digit %c ", hexaDecimal[i]);                              return 0;
         }
         i++;
    }

    return 0;
}

I keep getting errors such as:

part1_V2.c: In function ‘hex_to_binary’:
part1_V2.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
part1_V2.c:22: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’    token
part1_V2.c:26: error: stray ‘\342’ in program
part1_V2.c:26: error: stray ‘\200’ in program
part1_V2.c:26: error: stray ‘\234’ in program
part1_V2.c:26: error: stray ‘\342’ in program
part1_V2.c:26: error: stray ‘\200’ in program
part1_V2.c:26: error: stray ‘\235’ in program
part1_V2.c:59: error: expected ‘{’ at end of input

I got my code working, but now I'm have trouble with the output:

The textfile I pass to the main function contains:

"1283" (line1) "5105" (next line)

These are the hexadecimal values on the file. So when I run the program I get output:

Equivalent binary value: 0001001010000011
Invalid hexadecimal digit
Invalid hexadecimal digit
Equivalent binary value: 0101000100000101

Why am I getting the invalid hexadecimal digit output? Is it because it is trying to convert "\n" or empty space to binary?

Upvotes: 0

Views: 3129

Answers (4)

DiJuMx
DiJuMx

Reputation: 524

As mentioned in the comments, you are missing a ; on line 5.

Missing a ; can cause a cascade of errors, due to the compiler looking for something to end the statement (which wasn't ended by the ;).


Bearing that in mind, attempt to fix each error in turn. The error messages always look really technical, but if you stop and read it it gives a lot of information.

So for your first error, on line 8, it's stating that it was expecting something be the {. Since this is the start of the main function, the contents of lines 7-8 are correct, so the error must be before these two lines.

Before your main function is a function prototype, which...aha...is missing the semicolon.

In regards to the errors saying stray \xxx in program, this is likely because you've copied the scanf("%s", argv[1]) line from a word document or website, and it's copied a formatted pair of quote marks.

To fix it, just delete the quotes and type them by hand.

Upvotes: 0

WolfCoder
WolfCoder

Reputation: 137

Your first minor error is at the function definition at line 5. Add a semicolon at the end:

int hex_to_binary(char *argv[]);

Also, there isn't any use for it, so delete the line:

scanf(“%s”, argv[1]);

And rewrite the while loop:

while(hexaDecimal[i]!='\n' && hexaDecimal[i]!='\0')
{
  switch(hexaDecimal[i])
  {
     ...
  }
  i++;
}

Note that i++ is within the loop. Plus the return 0 statement at the end is outside any function. Move it in.

Upvotes: 0

Vladyslav
Vladyslav

Reputation: 786

There are some syntax errors in your code. Try this modified code. If you are passing the right parameters to main() then it should work:

#include <stdio.h>

int hex_to_binary(char*);

int main(int argc, char *argv[])
{
    FILE *file;
    file = fopen(argv[1],"r");
    char line[100];

    while(!feof(file)){
        fgets(line,100,file);
        hex_to_binary(line);
    }
    fclose(file);
    getchar();
    return 0;
}

int hex_to_binary(char* hex_string)
{
    int i=0;
    printf("\nEquivalent binary value: ");
    while(hex_string[i])
    {
         switch(hex_string[i])
         {
             case '0': printf("0000"); break;
             case '1': printf("0001"); break;
             case '2': printf("0010"); break;
             case '3': printf("0011"); break;
             case '4': printf("0100"); break;
             case '5': printf("0101"); break;
             case '6': printf("0110"); break;
             case '7': printf("0111"); break;
             case '8': printf("1000"); break;
             case '9': printf("1001"); break;
             case 'A': printf("1010"); break;
             case 'B': printf("1011"); break;
             case 'C': printf("1100"); break;
             case 'D': printf("1101"); break;
             case 'E': printf("1110"); break;
             case 'F': printf("1111"); break;
             case 'a': printf("1010"); break;
             case 'b': printf("1011"); break;
             case 'c': printf("1100"); break;
             case 'd': printf("1101"); break;
             case 'e': printf("1110"); break;
             case 'f': printf("1111"); break;
             default:  printf("\nInvalid hexadecimal digit %c ", hex_string[i]);
        }
        i++;
    }
    return 0;
}

Upvotes: 0

lnvd
lnvd

Reputation: 137

It seems like there are a couple of non-ASCII characters in your code, e.g. and , which should be ".

What's left are syntax and type errors.

Upvotes: 1

Related Questions