![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jun 2005
Location: Kent (old) England
Posts: 34
Rep Power: 0
![]() |
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; any ideas???
__________________
I use MSVC6.0, on XP Pro Happy Programming :D http://www.danasoft.com/sig/NelliesSig.jpg |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Jun 2005
Location: Maryland, USA
Posts: 59
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#3 | |
|
Programmer
Join Date: Jun 2005
Location: Kent (old) England
Posts: 34
Rep Power: 0
![]() |
Quote:
__________________
I use MSVC6.0, on XP Pro Happy Programming :D http://www.danasoft.com/sig/NelliesSig.jpg |
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Jun 2005
Location: Maryland, USA
Posts: 59
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Jun 2005
Location: Kent (old) England
Posts: 34
Rep Power: 0
![]() |
i'll give it a go
__________________
I use MSVC6.0, on XP Pro Happy Programming :D http://www.danasoft.com/sig/NelliesSig.jpg |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|