1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
#include <stdio.h>
#define LONG 0
#define WORD 1
#define BYTE 2
main(argc,argv)
int argc;
char *argv[];
{
signed long snum;
unsigned long num;
unsigned short word;
unsigned char byte;
double dbl;
int mode=LONG;
int arg=0;
int f;
char *binary();
char *ascii();
if(argc==1)
{
printf("%s: usage %s N1 [..Nn]\n",argv[0],argv[0]);
exit(-1);
}
for(f=1;f<argc;f++)
{
num=(unsigned long)strtoul(argv[f],(char **)NULL,0);
snum=(signed long)num;
if (num<0x100)
mode=BYTE;
else if (num<0x10000)
mode=WORD;
else
mode=LONG;
switch(mode)
{
case LONG:
memcpy(&dbl,&num,sizeof(unsigned long));
printf("Original : %s\n",argv[f]);
printf("Octal : %o\n",num);
printf("Uns. Dec : %u\n",num);
printf("Sgn. Dec : %ld\n",snum);
printf("Hex : %8.8X\n",num);
printf("Binary : %s\n",binary(num,32));
printf("Ascii : %s\n",ascii(num,4));
printf("Double : %f\n\n",dbl);
break;
case WORD:
word=(unsigned short)num;
printf("Original : %s\n",argv[f]);
printf("Octal : %o\n",word);
printf("Uns. Dec : %u\n",word);
printf("Sgn. Dec : %ld\n",(signed short)snum);
printf("Hex : %4.4X\n",word);
printf("Binary : %s\n",binary(word,16));
printf("Ascii : %s\n\n",ascii((unsigned long)word,2));
break;
case BYTE:
byte=(unsigned char)num;
printf("Original : %s\n",argv[f]);
printf("Octal : %o\n",byte);
printf("Uns. Dec : %u\n",byte);
printf("Sgn. Dec : %ld\n",(signed char)snum);
printf("Hex : %2.2X\n",byte);
printf("Binary : %s\n",binary(byte,8));
printf("Ascii : %s\n\n",ascii((unsigned long)byte,1));
break;
}
}
}
char *binary(i,w)
unsigned long i,w;
{
static char buff[1024],*p;
unsigned long c=w,b=1<<(w-1);
int first=1;
p=buff;
for(;c--;b=b>>1)
{
if (!((c+1)%4)&&!first)
*p++=' ';
if(i&b)
*p++='1';
else
*p++='0';
first=0;
}
*p='\0';
return(buff);
}
char *ascii(num,l)
unsigned long num;
{
static char s[5];
int f;
unsigned char c;
s[l]=0;
for(f=0;f<l;f++)
{
c=num&0xff;
if ((c>=32)&&(c<127))
s[(l-1)-f]=c;
else
s[(l-1)-f]='.';
num=num>>8;
}
return(s);
}
|