My program should take in 8-bit binary numbers such that the represented ASCII c
ID: 3567324 • Letter: M
Question
My program should take in 8-bit binary numbers such that the represented ASCII character should be printed. For example input 01001110 01011010 should give NZ. It should be possible to take in a whole file consisting a bunch of such 8bit numbers. Here's what I got, it is compilable, but not runable:
# include <iostream>
# include <ios>
# include <cmath>
# include <vector>
}
int main(){
std::vector <int> t (8);
std::cin >> std::noskipws;
char next;
char a;
int s=0;
while(std::cin >> next){
for(int i=0; i<8; ++i)
std::cin >> t[i];
for(int i=0; i<8; ++i)
s+=t[i]*pow(2,i);
a=s;
std::cout << a;
}
//./nzz_decoder < nzz.in
return 0;
}
Explanation / Answer
// QA_6151899.cpp : Defines the entry point for the console application.
//
#include
"stdafx.h"
#include
<iostream>
#include
<ios>
#include
<cmath>
#include
<vector>
using
namespace std;
int
main()
{
vector<
int> t(8);
char next;
while(next != ' ')
{
for(int i = 0; i < 8; i++)
{
char ch;
cin >> ch;
t[i] = ch -
'0';
}
for(int i = 0; i < 8; i++)
cout << t[i] << endl;
int s = 0;
for(int i = 0, j = 7; i < 8, j >= 0; i++, j--)
s += (t[j]* (
int)pow(2.0, i));
char a = s;
cout << a << endl;
cin >> next;
}
cout <<
"hi" << endl;
cout << endl;
system(
"pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.