diff options
author | Ian C <ianc@noddybox.co.uk> | 2010-11-08 10:13:35 +0000 |
---|---|---|
committer | Ian C <ianc@noddybox.co.uk> | 2010-11-08 10:13:35 +0000 |
commit | e66d6a836c6e802b80b10578a9aea86fb90f8477 (patch) | |
tree | bf9e9abbc70f76ff26c277d24fef2159f48d66e7 /asc.c | |
parent | 9a9946619c1f620c582cc67f4ae08351f868ff03 (diff) |
Tidied up asc.c
Diffstat (limited to 'asc.c')
-rw-r--r-- | asc.c | 110 |
1 files changed, 58 insertions, 52 deletions
@@ -1,73 +1,79 @@ +#include <stdlib.h> #include <stdio.h> +#include <string.h> -main(argc,argv) -int argc; -char *argv[]; - +static char *binary(unsigned int i, unsigned int w) { - int f,l,r; + static char buff[1024]; + char *p; unsigned int c; + unsigned int b; + + c = w; + b = 1<<(w-1); - char *binary(); + p = buff; - if(argc==1) + for(c = w; c--; b = b>>1) + { + if(i&b) + { + *p++='1'; + } + else { + *p++='0'; + } + } + + *p='\0'; + + return buff; +} + +int main(int argc, char *argv[]) +{ + int f; + int l; + int r; + unsigned int c; + + if(argc == 1 || (argc == 2 && strcmp(argv[1],"-c") == 0)) + { printf("%s: usage %s string1 [..stringn] | -c num1 [.. num n]\n", argv[0],argv[0]); - exit(-1); - } + exit(EXIT_FAILURE); + } - if(strcmp(argv[1],"-c")) + if(strcmp(argv[1],"-c") != 0) + { + for(r = 1 ;r < argc; r++) { - for(r=1;r<argc;r++) + l = strlen(argv[r]); + + for(f = 0; f < l; f++) { - l=strlen(argv[r]); + c = (unsigned int)*(argv[r]+f); - for(f=0;f<l;f++) - { - c=(unsigned int)*(argv[r]+f); - printf("%c (%3d - 0x%2.2X %%%s)\n", - ((*(argv[r]+f) > 31) ? (*(argv[r]+f)) : '?') - ,c,c,binary(c,8)); - } + printf("%c (%3u - 0x%2.2X %%%s)\n", + (*(argv[r]+f) > 31) ? (*(argv[r]+f)) : '?', + (unsigned int)(unsigned char)c, + (unsigned int)(unsigned char)c, + binary(c,8)); } } + } else - { - if(argc==2) - { - printf("%s:usage %s -c num1 [.. num n]\n",argv[0],argv[0]); - exit(-1); - } + { for(f=2;f<argc;f++) - { - c=(char)strtol(argv[f],NULL,0); + { + c = (unsigned int)strtol(argv[f], NULL, 0); - printf("%c (%3d - 0x%2.2X %%%s)\n", - (c>31) ? (char)c : '?', - c,c,binary(c,8)); - } + printf("%c (%3u - 0x%2.2X %%%s)\n", + c>31 ? (char)c : '?', + c, c, binary(c,8)); } - -} - - -char *binary(i,w) -unsigned int i,w; - -{ - static char buff[1024],*p; - unsigned int c=w,b=1<<(w-1); - - p=buff; - - for(;c--;b=b>>1) - if(i&b) - *p++='1'; - else - *p++='0'; - - *p='\0'; + } - return(buff); + return EXIT_SUCCESS; } |