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
|
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
unsigned int div256(unsigned char tab[], unsigned int nbtab)
{
unsigned int A, R ,i;
A = tab[0];
R = 0;
for(i = 0; i<nbtab;i++)
{
A = 10*R +tab[i];
tab[i] = A/256;
R = A%256;
}
return R;
}
void ascii2binary (unsigned char tab[], unsigned int nbtab, unsigned char dest[],
unsigned int nbdest)
{
unsigned int i;
// Si les données de départ sont en caractères :
for(i=0 ; i<nbtab ;i++ ) tab[i] = tab[i]-'0';
for(i=0 ; i<nbdest;i++ ) dest[i] = div256(tab,nbtab);
}
void main (int args, char * argv[])
{
int indice;
unsigned char tab[40];
unsigned char dest[40];
if(args != 2)
{
printf("erreur\n");
printf("usage: call program with string in params");
exit(1);
}
indice = 0;
while(argv[1][indice] != '\0')
{
indice++;
}
printf("le parametre %s contient %d lettres\n", argv[1], indice);
for(int c=0; c<indice;++c)
{
tab[c]=(char)argv[1+c];
}
printf("tab[] contient:%s\n", argv[1]);
int lentab = sizeof tab /sizeof *tab;
int lendest = sizeof dest /sizeof *dest;
ascii2binary(tab,lentab,dest,lendest);
printf(" verification : \n");
for (int i=0; i<lentab;++i)
printf("apres conversion tab[] contient:%c\n", tab[i]);
for (int j=0; j<lendest;++j)
printf("apres conversion dest[] contient:%c\n", dest[j]);
system("PAUSE");
} |
Partager