#include /* putc() */ #include /* CHAR_BIT */ #define EXIT_PASS (0) #define TSYM 'X' #define FSYM '-' #define PutC(c) (putc((char)(c),stdout)) #define DEFAULT_BASE (10) /* Must be 2 through 36 */ #define PutD(d) (PutC( (char) ((d)<10)?('0'+(d)):('A'+(d)-10) )) #define Put_u(n) (Put_ubase((n), DEFAULT_BASE)) #define PutH_u(n)(Put_ubase((n), 16)) void Put_ubase(unsigned int n, int base) { /* NOTE: 2 <= base <= 36 */ unsigned int m; int i; /* Determine how many digits there are */ for (m = 1; n/m >= base; m*=base ) /* EMPTY LOOP */; /* Print out the digits one-by-one */ do { for(i = 0; n >= m; i++ ) n = n - m; PutD(i); m = m / base; } while ( m >= 1 ); } int Putf_u(int d, int w, int how) { int digits; int wt; int chars; /* determine how many digits are required */ for(digits = 0, wt = 1; d/wt >= 10; digits++) wt *= 10; /* how many characters actually printed */ chars = 0; /* if how = 0, left justify the result */ /* if how = 1, right justify and pad with spaces */ /* if how = 3, right justify and pad with leading zeros */ if (how) { while ( (w-chars) > digits ) { PutC( (3==how)? '0': ' '); chars++; } } /* Output basic integer */ Put_u(d); chars += digits; /* Pad any remaining space in output field */ while ( w-chars ) { PutC(' '); chars++; } return chars; } int PutByte(void *address) { unsigned char mask; unsigned char *ptr; int chars; ptr = (unsigned char *) address; for (chars = 0, mask = 1 << (CHAR_BIT - 1); mask; mask >>= 1) { PutC((*ptr & mask)? '1':'0'); chars++; } return chars; } int PutBytes(void *addr, int bytes) { unsigned char *ptr; int i, count; for (i=count=0, ptr = (unsigned char *) addr; i < bytes; i++, ptr++) count += PutByte(ptr); return count; } int main(void) { unsigned int i; for(i = 0; i < 10; i++) { Putf_u(i, 3, 3); PutC(':'); PutBytes(&i, sizeof i); PutC('\n'); } return EXIT_PASS; }