Original 20051017 tarball
[acx-mac80211.git] / pktgen / printhex.c
blobe9b8b6eb00de4ee139531228f2fcb1afdeba63ea
1 /* prints hex string argv in binary to stdout */
3 #include <stdio.h>
4 #include <ctype.h>
6 int char2hex(char c) {
7 c = tolower((unsigned int)c);
8 if (isdigit((unsigned int)c)) {
9 return c-'0';
10 } else if (c >= 'a' && c <= 'f') {
11 return c-'a'+10;
12 } else {
13 return -1;
17 void spew_bytes(const char *s) {
18 char buf[256];
19 int n1,n2;
20 char *p;
21 again:
22 p = buf;
23 while(p < (buf+sizeof(buf))) {
24 n1 = char2hex(*s++);
25 if(n1 < 0) break;
26 n2 = char2hex(*s++);
27 if(n2 < 0) break;
28 *p++ = (n1<<4) + n2;
30 if(p!=buf) write(1, buf, p-buf);
31 if( (n1|n2)>=0 ) goto again;
34 int main(int argc,char** argv) {
35 while(--argc > 0)
36 spew_bytes(*++argv);
37 return 0;