Binary to ASCII. Improvements?

This is a program I made while learning about static type casting. The user must type in a binary byte such as 00011011 and the program will return the ASCII equivalent (unless the ASCII character is undisplayable, such as a NULL character).

What do you think of it and mainly, what improvements do you think I can make on it?

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
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
	int			bit[8] = { 128, 64, 32, 16, 8, 4, 2, 1 };
	int			bitA = 0;
	int			i;
	char		usr_byte[9];

	cout	<<	"Enter byte: ";
	cin		>>	setw(9)	>>	usr_byte;

	for(i=0;i<=8;i++) {
		if(usr_byte[i]=='1') {
			bitA+= bit[i]; 
		}
	}

	cout	<<	"Byte Numerical Value: "	<<	bitA	<<	"\n";
	char ASC = static_cast<char>(bitA);

	cout	<<	"ASCII Character Value: "	<<	ASC		<<	"\n";

	return 0;
}



Enter byte: 01000001
Byte Numerical Value: 65
ASCII Character Value: A
Not really an improvement but maybe an extension :

Offer output in Hexadecimal/Octal as well without using the cout manipulator

i.e.
Enter byte: 010000001
Byte Decimal Value : 65
Byte Octal Value : 101
Byte Hexadecimal Value : 41
ASCII Character value : A

by cout manipulator i mean
 
cout << octal


etc.
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
//changed some formatting
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
	// int bit[8] = { 128, 64, 32, 16, 8, 4, 2, 1 }; //not needed
	int bitA = 0;
	// int i; //we don't need to define i in this scope
	char usr_byte[9];

	cout << "Enter byte: ";
	cin >> setw(9) >> usr_byte;

	//changed the loop a bit, we didn't need usr_byte[8]
	//i is now defined only inside the loop
	//(removed a few braces and changed a few "" to '')
	for( int i = 0; i < 8; i++ )
		if( usr_byte[i] == '1' )
			bitA += 1 << i; //look up left and right shift operators, i.e << and  >>
		else if( usr_byte[i] != '0' ) {
			cout << "Invalid didgit at position " << i << '\n';
			break;
		}

	cout << "Byte Numerical Value: " << bitA << '\n';
	// char ASC = static_cast<char>(bitA); //you don't need this variable
	cout <<	"ASCII Character Value: " << static_cast<char>(bitA) << '\n'; //just put the cast in here

	return 0;
}
Topic archived. No new replies allowed.