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