/* dec2bin.c -- convert decimal integer on stdin to binary on stdout
 * Andy Isaacson <adi@acm.org>  http://web.hexapodia.org/~adi/dec2bin.c
 * March 18, 2001
 * Requires GNU MP library; tested with version 3.1.1
 * I assert no copyright on this program; it is in the public domain unless
 * the GNU MP license requires otherwise (I don't know if it does).
 */
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>

int main(void)
{
    mpz_t n, q;
    int i, len;
    unsigned char *buf;

    mpz_init(n); mpz_init(q);

    mpz_inp_str(n, stdin, 0);

    len = mpz_sizeinbase(n, 256);
    buf = malloc(len+1);
    for(i=len-1; i>=0; i--)
	buf[i] = mpz_tdiv_qr_ui(n, q, n, 256);
    fwrite(buf, len, 1, stdout);
    fflush(stdout);
    fprintf(stderr, "%d bytes written\n", len);
    return 0;
}
