View difference between Paste ID: QZkQ1ELq and ZVn0kw5Y
SHOW: | | - or go back to the newest paste.
1
// Now handles two's compliment negative numbers, though I'm not convinced that it
2
// handles the codes correctly, certainly not UTF-8 encoding.
3
#include <stdio.h>
4
#include <stdlib.h>
5
#include <conio.h>
6
// Forward declaration of function:
7-
// The \0 sequence is an escape character which I think means 'null' or end of string:
7+
8
// Global variables:
9
static char x[2]=" \0";
10
static char buffer='\0';
11
static char run=0;
12
static char i=0;
13
// This is used as a boolean so that the negative numbers are represented
14
// correctly in binary:
15
static char twoscompliment=0;
16-
    // Clears screen and sets welcome message:
16+
17-
    // system("cls"); works okay with Code::Blocks on Windows, but doesn't with Visual Studio
17+
18
static char binary[8]="00000000";
19
int main()
20
{
21
    // Clears screen and sets welcome message (system (); might not work with Visual Studio 2010
22
    // but is fine with Code::Blocks):
23
    system("cls");
24
    printf("Welcome to this ascii and binary conversion programme.\n");
25
    printf("Please enter one character only and press the ENTER key.\n");
26
    printf("Entering two or more characters may crash this programme,\n");
27
    printf("or cause unusual results! (C) MMXII Donkeysoft.\n\n");
28
    // Tells the main element of the program to start, ie, everything in the
29
    // while() loop:
30
    run=1;
31
    while(run)
32
    {
33
        // Prompts for user input and gets the first two characters entered
34
        // by the user and stores in x at location zero and one:
35
        printf("\nEnter a character (press ENTER twice to exit)\nD:\\> ");
36
        x[0]=getchar(); x[1]=getchar();
37
        // Tests for condition to exit programme:
38
        if(x[0]=='\n' || x[0]=='\0')
39
        {
40
            // Says goodbye and switches off boolean to run:
41
            printf("\nGoodbye!");
42
            run=0;
43
            return 0;
44
        }
45
        // Outputs the character's ASCII value in decimal:
46
        printf("Character value in decimal: %d\n",x[0]);
47
        // Tests for negative numbers:
48
        if(x[0]<0)
49
        {
50-
                if(a)
50+
            twoscompliment=1;
51
            x[0]=x[0]+1;
52
        }
53
        else
54
            {
55
                twoscompliment=0;
56
            }
57
        // Sets each bit as appropriate:
58
        for(i=8;i>0;i=i-1)
59
            {
60
                // Stores the remainder of x[0] modulo 2 into a, so it will be
61
                // either 1 or zero, and then divides x[0] by 2:
62
                a=x[0]%2;
63
                x[0]=x[0]/2;
64
                // Tests to see if a is true or not:
65
                if(a && !twoscompliment)
66
                {
67
                    binary[i-1]='1';
68
                }
69
                else
70
                    if (!a && !twoscompliment)
71
                    {
72
                        binary[i-1]='0';
73
                    }
74
                else
75
                    if (a && twoscompliment)
76
                    {
77
                        binary[i-1]='0';
78
                    }
79
                else
80
                    if (!a && twoscompliment)
81
                    {
82
                        binary[i-1]='1';
83
                    }
84
            }
85
            // Now will output the contents of the character array called binary
86
            // as a string, containing ones and zeros for each bit set:
87
            printf("Character value in binary: %s\n",binary);
88
            if(twoscompliment)
89
            {
90
                printf("This is in two's compliment.\n");
91
            }
92
            printf("This reads as follows:\n");
93
            a=0;
94
            for(i=8; i>0; i=i-1)
95
            {
96
                printf("Bit %d is ",a);
97
                if(binary[i-1]=='1')
98
                {
99
                    printf("TRUE\n");
100
                }
101
                else
102
                    {
103
                        printf("FALSE\n");
104
                    }
105
                a=a+1;
106
            }
107
    }
108
}