Printing bits from unsigned char (BYTE) in C.

Anybody knows what numbers made in our computer if you have experience in programming. But did you have try to print what ‘unsigned char’ or ‘BYTE’, 1 byte data to bits?
‘unsigned char’ and ‘BYTE’ contain a number in ranged 0 to 255 in 8 bits. 0000:0000 to 1111:1111. Every numbers made in bits, a digital switched. So I made this expression for simple C code.

char* uchar2bits( unsigned char uc )
{
    static char outstr_uc[10];
    int cnt;
    unsigned char dec;
    int que;

    memset( outstr_uc, 0, 10 );
    outstr_uc[4] = ':';

    que = 0;

    for ( cnt = 7 ; cnt >= 0 ; cnt-- )
    {
        dec = uc >> cnt;

        if ( dec & 1 )
            outstr_uc[que] = 1 + '0';
        else
            outstr_uc[que] = 0 + '0';

        que++;
        if ( que == 4 )
            que++;
    }

    return outstr_uc;
}

Leave a Reply