0

I'm looking for the least amount of code in C, in order to convert a char to int, where it flags -1 (or any error flag) if the char is not a valid hex digit.

here's what I came up with, is there a shorter way?

// input example
char input = 'f';   

// conversion segment
int x = input - '0';    
if (!((x >= 49 && x <= 54) || (x >= 0 &&  x <= 9))) x = -1;
if (x > 9) x -= 39;

// test print   
printf("%d", x);
Austin
  • 6,921
  • 12
  • 73
  • 138
  • And posters act like they forgot about `'A'` – M.M Mar 30 '16 at 03:30
  • What do you want the character `'a'` to be converted to? – kaylum Mar 30 '16 at 03:33
  • @kaylum `10` presumably – M.M Mar 30 '16 at 03:35
  • I'd like 'a' to be converted to 10, 'f' to be 15, '0' to '9' to be their integer equivalents, and any other char not in those ranges to return -1. Trying to see the least amount of lines this can be done in. – Austin Mar 30 '16 at 03:35
  • There isn't a standard function that does that directly. You could use `if (sscanf(&c, "%1x", &x) != 1) x = -1;` at a pinch. Alternatively, you can play is `isdigit()` and `isxdigit()` (and maybe `isupper()` or `islower()`) from `` to work out what to do. Or create a lookup table for possible (unsigned) `char` values initialized with `-1` except for the valid hex digits. – Jonathan Leffler Mar 30 '16 at 03:36
  • I only ask because in the proposed option `'a' - '0'` gives `49`. So it's definetely not the shortest way as it is not a solution. – kaylum Mar 30 '16 at 03:38
  • don't use hardcode values like 48, 57, 97 or 102. Use 'a', 'z', 'A', 'Z'... instead – phuclv Mar 30 '16 at 03:41
  • sorry kaylum, I corrected the ascii values. Why did someone vote to close? I don't see another question that addresses this and I'm sure someone looking to shorten up their code may find it useful. – Austin Mar 30 '16 at 03:44
  • 1
    "shorten up the code" is something you do for Obfuscated C contests. Any actual programmer would just write normal code using obvious logic. – M.M Mar 30 '16 at 03:47
  • I'm voting to close this question as off-topic because writing shortest code may be fun, but is useless. – ugoren Mar 30 '16 at 13:42

2 Answers2

1

This code assumes ASCII and converts all 256 characters codes into 256 different codes, partially '0'-'9' 'A'-'F' map to 0,1,...15.

For additional tricks and simplification see the post

unsigned char ch = GetData(); // Fetch 1 byte of incoming data;
if (!(--ch & 64)) {           // decrement, then if in the '0' to '9' area ...
  ch = (ch + 7) & (~64);      // move 0-9 next to A-Z codes
}
ch -= 54;                     // -= 'A' - 10 - 1
if (ch > 15) { 
  ; // handle error
}
Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

Try this function:

isxdigit(c);
eyalm
  • 3,366
  • 19
  • 21
  • I knew there was a way! By the way for anyone else it requires `` – Austin Mar 30 '16 at 04:01
  • Actually this would have be more nice if it returned the int equivalent value rather than a 1, but still shortens things up a bit. – Austin Mar 30 '16 at 04:03
  • This does not convert per the post's title "convert hex char to int", it simply tests (true/false) if a character is a hexadecimal character - and takes a 256 bit/byte table to do it. – chux - Reinstate Monica Mar 30 '16 at 04:03
  • I agree. It only tests. But, as i understood it, the questions was also about getting an error. – eyalm Mar 30 '16 at 05:16