Solved

How can I change a char to a binary!

Posted on 2003-11-23
3
520 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 fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

827 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