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 148 149 150 151 152
|
#include <iostream>
#include <string>
#include <string>
#include "windows.h"
#include <tchar.h>
void clearScreen()
{
HANDLE output_handle = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD bytes_write, size;
COORD coord = {0, 0};
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(output_handle, &csbi);
size = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(output_handle, ' ', size, coord, &bytes_write);
SetConsoleCursorPosition(output_handle, coord);
}
int biToDec(std::string a)
{
int temp = 0;
for(unsigned int i = 0; i < 8; i++)
{
if(temp > 0 ||a[i] == '1')
{
switch(a[i])
{
case '0':
temp *= 2;
break;
case '1':
temp = (temp * 2) + 1;
break;
}
}
}
return temp;
}
int main()
{
SetConsoleTitle(_T( "Binary Converter by: Robert Cruz" ));
char cChar;
do
{
std::string biNum;
bool biCorrect = false;
std::cout << "Enter an 8 bit binary string: ";
std::cin >> biNum;
std::cout << biNum.length();
system("pause");
for(unsigned int i = 0; i < biNum.length(); i++)
{
if(biNum.substr(i, 1) == " ")
std::cout << i << " ";
}
system("pause");
while(!(biCorrect))
{
// Program can do calculations like 11; 11 = 3
while(biNum.length() < 8)
{
// Inserts 0 in front of string till it has a total 8 character in order to read.
for(unsigned int i = 0; i < 8 - biNum.length(); i++)
biNum.insert(0, 1, '0');
}
// Checks to see if the string is a multiple of 8, in which case can be calculation.
while(biNum.length() % 8 != 0)
{
std::cout << "Please enter an 8 bit binary string consisting only of 0s and 1s.";
std::cin.ignore();
std::cin.get();
clearScreen();
std::cout << "Enter an 8 bit binary string: "; std::cin >> biNum;
}
for(unsigned int i = 0; i < biNum.length(); i++)
{
if(biNum[i] == '0' || biNum[i] == '1')
biCorrect = true;
else
{
biCorrect = false; i = 0;
std::cout << "Please enter an 8 bit binary string consisting only of 0s and 1s.";
std::cin.ignore();
std::cin.get();
clearScreen();
std::cout << "Enter an 8 bit binary string: "; std::cin >> biNum;
while(biNum.length() < 8)
{
for(unsigned int i = 0; i < 8 - biNum.length(); i++)
biNum.insert(0, 1, '0');
}
}
}
}
// Output
if(biNum.length() == 8)
{
std::cout << "The decimal value of your binary string is: " << biToDec(biNum) << std::endl;
std::cout << "The ASCII value of your binary string is: " << static_cast<char>(biToDec(biNum));
}
else
{
bool finish = false;
unsigned int j = 0;
std::string sentence;
while(!finish)
{
std::string tempStr;
while(j < biNum.length())
{
tempStr.assign(biNum, j, 8);
j = j + 8;
if(j == biNum.length())
finish = true;
break;
}
//std::cout << "The decimal value of your binary string is: " << biToDec(tempStr) << std::endl;
//std::cout << "The ASCII value of your binary string is: " << static_cast<char>(biToDec(tempStr)) << std::endl << std::endl;
sentence += static_cast<char>(biToDec(tempStr));
}
std::cout << "Your string read's: " << sentence;
}
std::cout << "\nDo you want to continue using this program? <y/n>: "; std::cin >> cChar;
std::cout << std::endl;
} while(cChar == 'y' || cChar == 'Y');
return 0;
}
|