Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How can I change a char to a binary!

Posted on 2003-11-23
3
Medium Priority
?
530 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 150 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

610 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