Solved

How can I change a char to a binary!

Posted on 2003-11-23
3
519 Views
Last Modified: 2010-04-21
With C langluage, I have a char, I want change it to a binary?
for example, char'0', change to binary is 11000.
how can I do it?
0
Comment
Question by:lakeryu
3 Comments
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 9806055
On other systems you would use the itoa() function, which allows you to convert an integer to an ASCII string by specifying thebase or radix (in your case, this would be 2 for binary). This is usually used to convert an integer to a base 10 (decimal) string, but can use anything from 2 to 36.

This page contains several methods to create your own atoi() function. If you search for a2i on this page, you will find the one that I would use:
http://dbforums.com/archives/t57204.html

Once you have this itoa() function, you would then call it like this:

char c = '0';
char outString[1024];

itoa((int) c, outString, 2);

To convert the character '0' to it's binary representation.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9808360
use strtol

 long int strtol(const char *nptr, char **endptr, int base);

result = strtol ( start, NULL, 2 );

man strtol ...
0
 
LVL 5

Accepted Solution

by:
g0rath earned 50 total points
ID: 9850056
#include <stdio.h>

char *int2bin( int aint, int width )
{
        char *buf;
        int j;
        int i = width;

        // room for a null
        buf = (char *)calloc(1, width+1);
        memset(buf,'0',width);

        while ( 1 )
        {
                j = aint % 2;
                aint /= 2;

                buf[i--] = j == 0 ? '0' : '1';

                if (aint == 0 || i == 0)
                        break;
        }
        return buf;
}

int main( int argc, char **argv )
{
        char achar;

        achar = 'z';

        printf("%s\n",int2bin(achar,16));
        return 0;
}

// this returns 00000000001111010
// mainly this is just to show that all you do is divide by 2 until you reach zero, and reverse the order that you
// recieved the remainders in

// just as easily you could change the base number to 8, for octal, but for 16, you would need some lookup table
// for the A,B,C,D,E,F values


0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question