groups

Link to this snippet:


Download to Code Collector

language: C
licence: Other

Printing Binary Representation of String

options: send to code collectorview all myztikjenz's snippets
// http://forums.devshed.com/showpost.php?p=2150495&postcount=10
#include <stdio.h>

void binaryOut (char c)
{
    unsigned char mask = 0x80; // 10000000 binary
    while (mask)
    {
        if (c & mask) putchar ('1');
        else putchar ('0');
        mask >>= 1;
    }
}

int main (int argc, char* argv [])
{
    char theString [] = "Your String to Print";
    char* thePointer = theString;
    unsigned wrapCounter = 0;
    while (*thePointer)
    {
        binaryOut (*thePointer++);
        putchar (' ');
        wrapCounter++;
        if (wrapCounter > 7) 
        {
            putchar ('\n');
            wrapCounter = 0;
        }
    }

    return 0;
}