I wrote a program converting binary to characters. I tried to have it where the code takes in a file and prints out the output but I keep getting an error on the output. Can someone take a look at it and see if they could fix the program.
Here is my code...
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
using namespace std;
void bin2char(const char* enter)
{
//Variables
int size=strlen(enter); //grabs length of string
int bin[8];
int tempChar = 0;
char character;
int a = 0;
for(int b=0; b<size/8; b++)
{
for(int c=0; c<8; c++)
{
bin[c]=(int)enter[a]-48;
a++;
}
int power[8];
int count = 7;
for(int d=0; d<8; d++)
{
power[d]=count;
count--;
}
for(int e=0;e<8;e++)
{
double r=bin[e];
double s=power[e];
tempChar += r* pow(2,s);
}
character = tempChar;
tempChar = 0;
cout << character;
}
}
int main()
{
char binaryCode[100000];
ifstream input("part1.rtf");
while(!input.eof())
{
input >> binaryCode;
}
cout << binaryCode << endl;
bin2char(binaryCode);
return 0;
}
Here is the data I saved to a text file.0100100001100101011011000110110001101111001000000101011101101111011100100110110001100100001000010000101000001010010100100110010101100001011011000110110001111001001011100010000000100000010001000110100101100100101011000110111010000100000011100110110111101101101011001010111010001101000011010010110111001100111001000000110110101101111011100100110010100100000011100000111001001101111011001100110111101110101011011100110010000111111000010100000101001001111010010110010111000100000001000000101011101101000011000010111010000100111011100110010000001110101011011100110100101110001011101010110010100100000011000010110001001101111011101010111010000100000011101000110100001100101001000000110011001101111011011000110110001101111011101110110100101101110011001110010000001110000011010000111001001100001011100110110010100111111000010100000101001010111011000010111001100100000011010010111010000100000010001010110110001101001011011110111010000100111011100110010000001110100011011110110100101101100011001010111010000100000010010010010000001110011011000010111011100111111
Edited by Alexander, 14 April 2011 - 04:42 PM.
(Code tags for preservation)


Sign In
Create Account


Back to top









