Refers to a single argument character passed by the user

I am trying to make a hexadecimal string converter. I want the argument to be passed at startup. While I have it.

int main(int argc, const char *argv[]){
    int i;
    for (i=0; i < strlen(argv[1]); i++) {
        printf("%s = %c\n",argv[1], (int)atol(argv[i]));
    }
    return 0;
}

but in printf()- argv[i]refer to all lines from this run. (ex "HexToString.exe", "ABCDEF0123456789"), but what I'm trying to do applies to all characters in the second argument, so `argv [1] [4] = 'E', but I don't think I can put the second bracket there, because its a one-dimensional array, it did not work before when I tested it.

The output, and it may seem strange, I want to be the equivalent of an ascii hex value. example. 'B' is 11, and ascii 11 is ♂.

So, I think im really trying to do exactly that ^ here, but with a string of these values. I know that this will not work as it is now (it will read them two characters at a time, convert and output), but for now I just want to be able to convert individual characters at a time.

double parentheses are also compiled, but an error after execution.

+4
source share
2 answers

You can arrange a loop for all characters

int main(int argc, const char *argv[])
 { int i; 

   printf("%s has \n",argv[1]);
   for (i=0; i < strlen(argv[1]); i++) 
   { 
      printf("%c = %d\n", argv[1][i],
      (argv [1][i] >= 'A')? (argv [1][i] - 'A' + 10 ) : (argv [1][i] - '0');
   } 
   return 0;
 }
+1
source

This did not work because the atolpointer is expecting char, try

char buffer[2];

buffer[0] = argv[1][4];
buffer[1] = '\0';

printf("%s = %c\n", argv[1], (int)atol(buffer));
+1
source

All Articles