-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin2txt.c
102 lines (92 loc) · 3.55 KB
/
bin2txt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
bin2txt converts 8-bit binary (in the form of 1s and 0s) to text
Copyright 2021 happysmash27
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <stdio.h>
int main(){
//Input Character
//int because that's what getc gives
//unsigned because to my knowledge, no characters are on negative values
unsigned int inc;
//Output Character
//char because out input is 8 bits, and char is also 8 bits
//unsigned because we are not outputting negative characters
unsigned char outc;
//Outloc is a char with the bit we want to change in outc
//This will be used with bitwise operators to set that bit
//unsigned char because our output character is an unsigned char
unsigned char outloc;
//It starts at the largest significant digit and goes down
/*To initialise this, we take 1 and shift it left 7 times,
to our largest significant digit*/
outloc = 1<<7;
/*To make it go down, we will shift it right once
using outloc >>= 1*/
/*Read the input characters until EOF occurs
(you can invoke this with ctrl-d)*/
while((inc = getc(stdin)) != EOF){
/*When outloc is shifted right the final time,
it will be equal to 0, with no bit selected.
When this occurs, we will output our character
and reset it back to the largest significant
digit*/
if (outloc == 0){
putchar(outc);
outloc = 1<<7;
}
if (inc == '1'){
//If our input character is 1...
/*Sets the bit in the same location as our bit
in outloc to 1...
by using the binary OR operator to set
bits to 1 if either of them is a 1, which
means that for all the 0s in my number comprising
zeros in every location except outloc, the
number will stay the same, but for that single
1 in outloc, this bit will be 1 no matter what,
since whether or not outc has a 1 in that
location, outloc will have a 1 to ensure
that at least one bit, from either side,
is a 1*/
outc |= outloc;
//Shift outloc right once
outloc >>= 1;
} else if (inc == '0'){
//If our input character is 0...
/*Sets the bit in the same location as out bit
in outloc to 0...
by using the binary ones complement operator
to make a number this bit is 0, and every
other one (which was 0 before) is now 1. Then,
use thebinary AND operator to set a bit to 1
only if BOTH are 1. Since all bits except the
one I want to set to 0 are 1 in my number, those
will stay the same no matter what, but that
one zero will ensure that whatever is in that
space is zero, because my complement number
does not have a 1 there to satisfy the AND
operator*/
outc &= ~outloc;
//Shift outloc right once
outloc >>= 1;
}
/*If the character is neither a 1 or a 0, don't
do anything and wait for the next character*/
}
/*Quick fix for the bug where if EOF is given immediately
after the end of the 8 bits representing a character,
that last character is not printed*/
//May use a different solution later
if (outloc == 0){
putchar(outc);
}
}