Jump to content

Binary To Characters Conversion

- - - - -

  • Please log in to reply
6 replies to this topic

#1
AntLaTech

AntLaTech

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Hello all,

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)


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Your binary appears to have the text "Hello world!" at the beginning, and then unrenderable text afterwords.

I would assume that binary data was from a rich text format document, there is no guarentee that it is plain-text, so converting it to binary and back again would not display anything useful.

As for your code,

I would replace any calls to pow with bitwise shifting, for example to increase a power of two by a power of two, you would do the following:
i <<= 1;
or
i = i << 1;
Otherwise you are relying on types that can be very imprecise when converting back to an integer.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
AntLaTech

AntLaTech

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
The file is plain text because I did it by hand and it came out to a nice little phrase. But when I use my program with the file it doesn't do more than give me the Hello World!

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
What I've meant, is your program appears to be working fine. Take for example this phrase I just made:
010101000110100001101001011100110010000001100001011100000111000001100101011000010111001001110011001000000111010001101111001000000110001001100101001000000111011101101111011100100110101101101001011011100110011100101110
Your program will output my phrase:
This appears to be working.
It appears whatever binary you were feeding it (part1.rtf) is not encoded plaintext, only the hello world part is, something must have been malformed in your binary example.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
AntLaTech

AntLaTech

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Oh ok...Well i'll try and see what is wrong with it...Thank you...

#6
rocketboy9000

rocketboy9000

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Make it a TXT.

#7
AntLaTech

AntLaTech

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Yeah that was the problem. I fixed it earlier today...Thank you though...




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users