#ident "@(#)int2bcd.c	1.2 5/24/96 11:29:57"
/*
/* Copyright John Moyer 1996
/* permission is granted to use this for any purpose provided proper
/* credit is given to the author
*/

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <math.h>

#include "bignum.h"

int int2bcd( const int x, BigNum *b)
{
int i, j, l;
char p[256];

if ( x == 0 )
  {
  memset(b->x,0,ISIZE);
  b->c = 0;
  b->isign = 0;
  return 0;
  }

sprintf(p,"%d",x);

i = 0;
b->isign = 1;
while (p[i] == ' ' || p[i] == '\t' )
  {
  if ( p[i] == '\0' )
    return 1;	/* end of string with no number */
  i++;
  if ( i >= ISIZE )
    return 2;	/* exceeded size */
  }
if ( p[i] == '+' )
  {
  i++;
  }
else if ( p[i] == '-' )
  {
  b->isign = -1;
  i++;
  }

l = strlen(&p[i]); /* length of string */

for( j = 0 ; p[i] != '\0' ; i++ )
  {
  if ( p[i] < '0' || p[i] > '9' )
    return 3;	/* not a digit */
  b->x[l-j-1] = p[i] - '0';
  j++;
  if ( j >= ISIZE )
    {
    b->c = ISIZE ;
    return 4;	/* too many digits */
    }
  }
b->c = j;
if ( j == 0 )
  b->isign = 0;
for ( ; j < ISIZE ; j++ )
  b->x[j] = 0;

return 0;
}

