-
Notifications
You must be signed in to change notification settings - Fork 1
/
ASCII-BINARY CONVERTER.html
100 lines (83 loc) · 6.07 KB
/
ASCII-BINARY CONVERTER.html
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
<HTML>
<HEAD>
</HEAD>
<BODY>
<CODE>
<PRE>
<FONT COLOR="#008000">/*
*PROGRAM: ASCII-BINARY CONVERTER
*AUTHOR: ANDY W. KAWA
*VERSION: 0.1.2017
*DATE: July 15, 2017.
*/</FONT>
<FONT COLOR="#0000ff">#include</FONT> <iostream>
<FONT COLOR="#0000ff">#include</FONT> <stdlib.h>
<FONT COLOR="#0000ff">#include</FONT> <iomanip>
<FONT COLOR="#0000ff">#include</FONT> <math.h>
<FONT COLOR="#0000ff">#include</FONT> <fstream>
<FONT COLOR="#0000ff">int</FONT> main()
{
system(<FONT COLOR="#ff0000">"color 0a"</FONT>);
system(<FONT COLOR="#ff0000">"title ASCII-BINARY CONVERTER v0.1.2017"</FONT>);
<FONT COLOR="#0000ff">int</FONT> digit[1000], len, base(2), rem[8], j(0), k(8);
std::string ch;
std::cout<<<FONT COLOR="#ff0000">"\n"</FONT><<<FONT COLOR="#ff0000">"\t \tASCII TO BINARY CONVERTER V0.1.2017"</FONT>;
std::cout<<<FONT COLOR="#ff0000">"\n"</FONT><<<FONT COLOR="#ff0000">"\t\t\tCREATED BY ANDY KAWA\n\n"</FONT>;
std::cout<<<FONT COLOR="#ff0000">"\nASCII(STRING): "</FONT>;
getline(std::cin,ch);
std::cout<<<FONT COLOR="#ff0000">"\n\n"</FONT>;
<FONT COLOR="#008000">//calc the length of ch</FONT>
len = ch.length();
<FONT COLOR="#0000ff">for</FONT>(<FONT COLOR="#0000ff">int</FONT> i=0; i<len; i++){
digit[i] = <FONT COLOR="#0000ff">static_cast</FONT><<FONT COLOR="#0000ff">int</FONT>>(ch[i]);
}
<FONT COLOR="#008000">//file stream section begins here</FONT>
std::ofstream writeFile;
writeFile.open(<FONT COLOR="#ff0000">"ASCII-BINARY.txt"</FONT>);
std::cout<<<FONT COLOR="#ff0000">"DECIMAL: "</FONT>;
<FONT COLOR="#0000ff">for</FONT>(<FONT COLOR="#0000ff">int</FONT> l=0; l<len; l++){
std::cout<<digit[l]<<<FONT COLOR="#ff0000">" "</FONT>;
writeFile<<digit[l]<<<FONT COLOR="#ff0000">" "</FONT>;
}
std::cout<<<FONT COLOR="#ff0000">"\n\n\n"</FONT>;
writeFile<<<FONT COLOR="#ff0000">"\n\n\n"</FONT>;
std::cout<<<FONT COLOR="#ff0000">"HEXADECIMAL: "</FONT>;
<FONT COLOR="#0000ff">for</FONT>(<FONT COLOR="#0000ff">int</FONT> n=0; n<len; n++){
std::cout<<std::hex<<digit[n]<<<FONT COLOR="#ff0000">" "</FONT>;
writeFile<<std::hex<<digit[n]<<<FONT COLOR="#ff0000">" "</FONT>;
}
std::cout<<<FONT COLOR="#ff0000">"\n\n\n"</FONT>;
writeFile<<<FONT COLOR="#ff0000">"\n\n\n"</FONT>;
std::cout<<<FONT COLOR="#ff0000">"BINARY: "</FONT>;
<FONT COLOR="#0000ff">for</FONT>(<FONT COLOR="#0000ff">int</FONT> m=0; m<len; m++){
<FONT COLOR="#0000ff">while</FONT>(digit[m]>0||j<8){
<FONT COLOR="#008000">//finds the remainder of the number</FONT>
rem[j] = digit[m]%base;
<FONT COLOR="#008000">//divides the number by the base value choosed</FONT>
digit[m] = digit[m]/base;
<FONT COLOR="#008000">//increments the array value</FONT>
j++;
}
<FONT COLOR="#0000ff">while</FONT>(k>0){
<FONT COLOR="#008000">//outputs the binaries</FONT>
std::cout<<rem[k-1];
writeFile<<rem[k-1];
<FONT COLOR="#008000">//decrements the array value</FONT>
k--;
}
std::cout<<<FONT COLOR="#ff0000">" "</FONT>; <FONT COLOR="#008000">//output the space after binary value here</FONT>
writeFile<<<FONT COLOR="#ff0000">" "</FONT>;
<FONT COLOR="#008000">//resets the iterators</FONT>
j = 0;
k = 8;
}
std::cout<<<FONT COLOR="#ff0000">"\n\n\n"</FONT>;
writeFile<<<FONT COLOR="#ff0000">"\n\n\n"</FONT>;
writeFile.close();
<FONT COLOR="#008000">//file stream section ends here</FONT>
<FONT COLOR="#0000ff">return</FONT> 0;
}
</PRE>
</CODE>
</BODY>
</HTML>