Solved

How can I change a char to a binary!

Posted on 2003-11-23
3
515 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now