0

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Landon
  • 21
  • 2
  • 8
  • Really? The compilation? – rendon Oct 19 '14 at 20:38
  • 1
    How about adding a `;` at the end of line 5? – rendon Oct 19 '14 at 20:39
  • Curious, what/who suggested to use `feof(file)` to determine EOF? – chux - Reinstate Monica Oct 19 '14 at 20:50
  • `scanf(“%s”,argv[1]` --> `scanf("%s",argv[1]`. Use a simple text editor rather than a document editor. – chux - Reinstate Monica Oct 19 '14 at 20:51
  • 1
    `int hex_to_binary(char *argv[])` --> `int hex_to_binary(char *line);` – BLUEPIXY Oct 19 '14 at 20:52
  • 1
    `scanf(“%s”,argv[1]);` remove this line. – BLUEPIXY Oct 19 '14 at 20:54
  • For the stray errors, this is a ***very*** common error when copying code from web pages, [PDF](https://en.wikipedia.org/wiki/Portable_Document_Format) documents, through chat (e.g. [Skype Chat](https://en.wikipedia.org/wiki/Features_of_Skype#Skype_chat) or [Facebook Messenger](https://en.wikipedia.org/wiki/Facebook_Messenger)), etc. The canonical question is *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen Apr 25 '23 at 22:48
  • The most common ones can ***positively*** (guesswork isn't required) ***be searched*** for (and replaced) using the regular expression `\x{00A0}|\x{200B}|\x{200C}|\x{FEFF}|\x{2013}|\x{2014}|\x{201C}|\x{201D}|\x{2212}|\x{00E4}|\x{FFFC}|\x{FFFD}|\x{2217}|\x{200C}|\x{202B}|\x{202A}|\x{FF1A}|\x{21B5}` (NO-BREAK SPACE, ZERO WIDTH SPACE, ZERO WIDTH NON-JOINER, ZERO WIDTH NO-BREAK SPACE, EN DASH, EM DASH, LEFT DOUBLE QUOTATION MARK, RIGHT DOUBLE QUOTATION MARK, MINUS SIGN, LATIN SMALL LETTER A WITH DIAERESIS, OBJECT REPLACEMENT CHARACTER, REPLACEMENT CHARACTER, ASTERISK OPERATOR, etc.). – Peter Mortensen Apr 25 '23 at 22:51
  • The two (different) double quotes in `scanf(“%s”,argv[1]);` are the only ones. At least as posted here. [LEFT DOUBLE QUOTATION MARK](https://www.charset.org/utf-8/9) (U+201C) and [RIGHT DOUBLE QUOTATION MARK](https://www.charset.org/utf-8/9) (U+201D). – Peter Mortensen Apr 25 '23 at 23:02
  • 1
    Related: *[Exit strategies for "chameleon questions"](https://meta.stackexchange.com/questions/43478/exit-strategies-for-chameleon-questions)* – Peter Mortensen Apr 28 '23 at 23:21
  • 1
    This ought to have been ***two questions***, one for the stray error and one for the problem with the hexadecimal conversion. They are two completely different problems. – Peter Mortensen Apr 28 '23 at 23:22

4 Answers4

1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lnvd
  • 137
  • 2
0

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;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vladyslav
  • 786
  • 4
  • 19
  • Thanks. Now I'm having an output issue. The textile I pass to the main function contains: 1283 and then next line: 5105. These are the hex values on the file. So when I run the program I get output: Equivalent binary value: 0001001010000011 nvalid hexadecimal digit Invalid hexadecimal digit Equivalent binary value: 0101000100000101 I'm wondering why I'm getting the invalid hexadecimal digit output? Is it because it is trying to convert "\n" or empty space to binary? – Landon Oct 19 '14 at 21:10
  • Just wanted to answer you about '\n'. I think so too. – Vladyslav Oct 19 '14 at 21:16
0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
WolfCoder
  • 127
  • 8
0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
DiJuMx
  • 524
  • 5
  • 10