-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexttobinary
147 lines (125 loc) · 4.46 KB
/
texttobinary
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <unordered_map>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
// this program takes user input and converts it into binary
// then it converts the binary into guitar tabs for meshuggah-styled riffs
// the program will output the guitar tabs for the input text
// make the guitar strings
vector<char> string1 = {};
vector<char> string2 = {};
vector<char> string3 = {};
vector<char> string4 = {};
vector<char> string5 = {};
vector<char> string6 = {};
// Function to print the guitar tabs
void printGuitarTab() {
// Print strings 1 to 6
cout << "|--";
for (char c : string1) {
cout << c;
}
cout << '\n';
cout << "|--";
for (char c : string2) {
cout << c;
}
cout << '\n';
cout << "|--";
for (char c : string3) {
cout << c;
}
cout << '\n';
cout << "|--";
for (char c : string4) {
cout << c;
}
cout << '\n';
cout << "|--";
for (char c : string5) {
cout << c;
}
cout << '\n';
cout << "|--";
for (char c : string6) {
cout << c;
}
cout << '\n';
cout << '\n'; // Separate sets with an extra newline for clarity
}
// Algorithm to populate string6 (intro)
void algorithmIntro(const string &binary, vector<char> &string6) {
for (int i = 0; i < binary.size(); ++i) { // 8 bits in a byte
string6.push_back(binary[i]);
string6.push_back('-'); // Add dash between each bit to mimic guitar tabs
}
}
// Fill strings 1 to 5 with '-' matching string6 length
void fillOtherStrings(vector<char> &string1, vector<char> &string2, vector<char> &string3,
vector<char> &string4, vector<char> &string5, const vector<char> &string6) {
for (size_t i = 0; i < string6.size(); ++i) {
string1.push_back('-');
string2.push_back('-');
string3.push_back('-');
string4.push_back('-');
string5.push_back('-');
}
}
// Clear all strings (reset for the next set)
void clearStrings(vector<char> &string1, vector<char> &string2, vector<char> &string3,
vector<char> &string4, vector<char> &string5, vector<char> &string6) {
string1.clear();
string2.clear();
string3.clear();
string4.clear();
string5.clear();
string6.clear();
}
// Main function
int main() {
// Hashmap: each letter is a key, and the value is the binary representation
unordered_map<char, string> textToBinary = {
{'A', "01000001"}, {'B', "01000010"}, {'C', "01000011"}, {'D', "01000100"},
{'E', "01000101"}, {'F', "01000110"}, {'G', "01000111"}, {'H', "01001000"},
{'I', "01001001"}, {'J', "01001010"}, {'K', "01001011"}, {'L', "01001100"},
{'M', "01001101"}, {'N', "01001110"}, {'O', "01001111"}, {'P', "01010000"},
{'Q', "01010001"}, {'R', "01010010"}, {'S', "01010011"}, {'T', "01010100"},
{'U', "01010101"}, {'V', "01010110"}, {'W', "01010111"}, {'X', "01011000"},
{'Y', "01011001"}, {'Z', "01011010"}, {' ', "00100000"}
};
// Get user input and convert to uppercase
string userText;
cout << "Enter text to convert to tabs. do not enter punctuation: ";
getline(cin, userText);
transform(userText.begin(), userText.end(), userText.begin(), ::toupper);
// Process input in chunks of 4 bytes
const int bytesPerLine = 4;
int currentByteCount = 0;
for (char c : userText) {
if (textToBinary.find(c) != textToBinary.end()) { // Ensure valid character
string binary = textToBinary[c];
algorithmIntro(binary, string6); // Populate string6 with binary and dashes
currentByteCount++;
if (currentByteCount == bytesPerLine) {
// Fill other strings to match string6
fillOtherStrings(string1, string2, string3, string4, string5, string6);
// Print the current set of guitar tabs
printGuitarTab();
// Clear strings for the next set
clearStrings(string1, string2, string3, string4, string5, string6);
// Reset the byte counter
currentByteCount = 0;
}
} else {
cout << "Character '" << c << "' is not supported.\n";
}
}
// Print any remaining data if input length is not a multiple of 4 bytes
if (currentByteCount > 0) {
fillOtherStrings(string1, string2, string3, string4, string5, string6);
printGuitarTab();
}
return 0;
}