Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 8th, 2005, 1:01 PM   #1
Nellie
Programmer
 
Nellie's Avatar
 
Join Date: Jun 2005
Location: Kent (old) England
Posts: 34
Rep Power: 0 Nellie is on a distinguished road
binary and ascii

I need now to be able to convert a ascii character into a binary value.

char chr;
int numchar;
numchar = (int)chr;
gives me the decimal ASCII value, i need to then convert the decimal value to binary.

any ideas???
__________________
I use MSVC6.0, on XP Pro

Happy Programming :D

http://www.danasoft.com/sig/NelliesSig.jpg
Nellie is offline   Reply With Quote
Old Jun 8th, 2005, 1:11 PM   #2
mitakeet
Programmer
 
mitakeet's Avatar
 
Join Date: Jun 2005
Location: Maryland, USA
Posts: 59
Rep Power: 4 mitakeet is on a distinguished road
ALL data stored in the machine is in binary, every bit of it (well, at least on almost all modern machines), everything else is just interpretation. Tomato, tomahto.
__________________

Free code: http://sol-biotech.com/code/.

It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Mitakeet

The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw
mitakeet is offline   Reply With Quote
Old Jun 8th, 2005, 1:27 PM   #3
Nellie
Programmer
 
Nellie's Avatar
 
Join Date: Jun 2005
Location: Kent (old) England
Posts: 34
Rep Power: 0 Nellie is on a distinguished road
Quote:
Originally Posted by mitakeet
ALL data stored in the machine is in binary
i need to put the binary value of the int into a edit box on a dialog.
__________________
I use MSVC6.0, on XP Pro

Happy Programming :D

http://www.danasoft.com/sig/NelliesSig.jpg
Nellie is offline   Reply With Quote
Old Jun 8th, 2005, 1:38 PM   #4
mitakeet
Programmer
 
mitakeet's Avatar
 
Join Date: Jun 2005
Location: Maryland, USA
Posts: 59
Rep Power: 4 mitakeet is on a distinguished road
Then you need to convert the binary representation into a string and display the string. This should get you started:

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

void printBits(unsigned long val){
    int i, end = sizeof(unsigned long) * 8;
    unsigned long tmp = (ULONG_MAX / 2) + 1;
    for (i=0; i<end; i++){
        if (tmp & val) putc('1', stdout);
        else putc('0', stdout);
        tmp >>= 1;
    }
}

int main(){
    int number;
    char buf[BUFSIZ];

    while (1){
        printf("Please enter a number to convert to binary: ");
        fgets(buf, BUFSIZ, stdin);
        rewind(stdin);
        number = atoi(buf);
        printBits(number);
        printf("\n");;
    }
    return 0;
}
__________________

Free code: http://sol-biotech.com/code/.

It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Mitakeet

The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw
mitakeet is offline   Reply With Quote
Old Jun 8th, 2005, 1:39 PM   #5
Nellie
Programmer
 
Nellie's Avatar
 
Join Date: Jun 2005
Location: Kent (old) England
Posts: 34
Rep Power: 0 Nellie is on a distinguished road
i'll give it a go
__________________
I use MSVC6.0, on XP Pro

Happy Programming :D

http://www.danasoft.com/sig/NelliesSig.jpg
Nellie is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:23 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC