Byte to Binary to Byte 

  

 

/* this code takes a BYTE and converts it 10,000,000 into a binary and then 10,000,000 times back to a BYTE. I know that there is a shorter way, but I don't know if there is a faster one. If there is please let me know. This is one of my first attempts at C/C++, so be gentle on me. */

char BinStr[8];

int AscStr;

void asc2bin(register int i);

void bin2asc();

void main()

    {

    unsigned int x;

    unsigned long j;

    unsigned char i;

    printf("Enter an integer from 0 to 255 ");

    scanf("%d",&i);

    printf("\nThe program will now solve 10,000,000 Binaries of %d and then convert back.\n",i);

    for (j=0; j<10000000; j++)

    asc2bin(i);

    x=8;

        while(x){

        x--;

        printf("%d",BinStr[x]);

    }

    printf("\n");

    for (j=0; j<10000000; j++)

    bin2asc();

    printf("%d\n",AscStr);

    scanf("%d",&i);

    _exit(0);

}

void asc2bin(register int num)

    {

    BinStr[7] = ((num & 0x80)>>7);

    BinStr[6] = ((num & 0x40)>>6);

    BinStr[5] = ((num & 0x20)>>5);

    BinStr[4] = ((num & 0x10)>>4);

    BinStr[3] = ((num & 0x8)>>3);

    BinStr[2] = ((num & 0x4)>>2);

    BinStr[1] = ((num & 0x2)>>1);

    BinStr[0] = (num & 0x1);

}

void bin2asc()

    {

    register int num;

    num = BinStr[7]<<7;

    num += BinStr[6]<<6;

    num += BinStr[5]<<5;

    num += BinStr[4]<<4;

    num += BinStr[3]<<3;

    num += BinStr[2]<<2;

    num += BinStr[1]<<1;

    num += BinStr[0];

    AscStr=num;

}