Solved

How can I change a char to a binary!

Posted on 2003-11-23
3
522 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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