-1

I was wondering if there was a way to read bytes (like this: \x00\x01\x02) from the command line in C.

For example:

#include <stdio.h>

int main(int argc, char *argv[]) {
  printf("%s", argv[1]);
  return 0;
}
user@UbuntuServer: ~/Code# gcc Program.c -o Program 
user@UbuntuServer: ~/Code# ./Program "\x48\x69"

Hiuser@UbuntuServer: ~/Code# ./Program "\x48\x69\x0a"

Hi
user@UbuntuServer: ~/Code#

Thanks!

speedxerox
  • 33
  • 6
  • Try e.g. `printf("%02hhx\n", argv[1][0]);` – Some programmer dude Nov 24 '18 at 09:01
  • @Someprogrammerdude Look at OPs input, that wont cut it. – Swordfish Nov 24 '18 at 09:05
  • Please read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Then edit your question to show us what output you want from the "input" you give. – Some programmer dude Nov 24 '18 at 09:14
  • All three inputs are wanted... The first input, or the one containing `gcc` is compiling, the second one is showing that `\x48\x69` is being printed to the terminal, but notice how there is no newline at the end? That's where the third input comes into play, showing that by adding `0A`, or newline, that it will also print a newline. – speedxerox Nov 24 '18 at 09:30

3 Answers3

2

Unless you use a library to parse regex strings like that, you'll need to parse the hex manually. Check out this answer (which has slightly different syntax but a similar function):

Hexadecimal string to byte array in C

Paul
  • 370
  • 1
  • 6
  • That should rather have been a comment. – Swordfish Nov 24 '18 at 09:17
  • Yes, but I can't comment until I have 50 reputation. Probably should have let someone else answer it I guess... – Paul Nov 24 '18 at 09:27
  • Didn't think about rep and being able to comment. I don't know what to do best in such a situation either. Gave you some, hope it helps ;) – Swordfish Nov 24 '18 at 09:29
  • In general, until you have enough rep to comment, it is best not to produce a 'not an answer' containing the comment. That's likely to get downvoted, which means it takes longer to reach the ability to comment. In this case, enough people seem to agree with your sentiment that you've now earned commenting privileges – Jonathan Leffler Nov 24 '18 at 09:44
  • @Paul Happy commenting :) – Swordfish Nov 24 '18 at 09:58
0

I would go for something like this:

int main(int argc, char **argv)
{
    char *buf = malloc(strlen(argv[1]) / 4 + 1);
    size_t i = 0;
    for (char *tok = strtok(argv[1], "\\x"); tok; tok = strtok(NULL, "\\x"))
    {
        sscanf(tok, "%02hhx", buf + i);
        i++;
    }
    buf[i] = '\0';
    printf("%s", buf);
    free(buf);
    return 0;
}
alamit
  • 392
  • 2
  • 10
0

I found the HEX to ASCII conversion functions on this thread, and modified it to suit my situation.

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

int hexToInt(char c) {
  int first = c / 16 - 3;
  int second = c % 16;
  int result = first * 10 + second;
  if(result > 9) {
    result--;
  }
  return result;
}

int hexToASCII(char c, char d) {
  int high = hexToInt(c) * 16;
  int low = hexToInt(d);
  return high + low;
}

int main(int argc, char *argv[]) {
  char* hexString = argv[1];
  char buf = 0;
  for(int i = 0; i < strlen(hexString); i++) {
    if(i % 2 != 0) {
      printf("%c", hexToASCII(buf, hexString[i]));
    } else {
      buf = hexString[i];
    }
  }
  return 0;
}
speedxerox
  • 33
  • 6
  • One observation: this assumes the alphabetic hex digits are in upper-case; it does not handle those in lower case correctly. It's an intriguing way of converting digits `0`..`9` and letters `A`..`F` to hex. The algorithm isn't obvious. Unfortunately, since it doesn't do any validation, it is not very resilient — I couldn't recommend it for general use, though given valid data, it does work. – Jonathan Leffler Nov 24 '18 at 10:04