CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2007
    Posts
    21

    Binary to ASCII converter

    This program convert the binary string back to the ASCII format. In case there is no ASCII counter part, it just prints the hexadecimal value of the character. A data file: zeros_and_ones.txt is given in the EXAM folder. Your program should at least be able to decode it back to plain text.

    A program take the binary string from keyboard entry
    A program read in the binary digits from a file and print its contents in ASCII
    A program that reads in the binary digits from a file, convert it to ASCII characters, and write it out into anther file.

    Code:
    // Project3.cpp 
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    void converByte(char byte[]);
    void converFile(char line_read[]);
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	char dummy;
    	char line_read[1000];
    	char byte[8];
    
    	ifstream infile;
    
    	infile.open("C:/zeros_and_ones.txt", ios::in);
    	infile.getline(line_read, 1000);
    
    	cout << "Read from the file: " << line_read << endl;
    
    	convertFile(line_read);
    
    	ofstream outfile("F:/*** 6/write.txt");
    	cout << endl;
    	cout << "Has been written to the file: \" F:/*** 6/write.txt\" " << endl;
    
    	cin.ignore();
    	cin >> dummy;
    
    }
    void converByte(char byte[])
    {
    	int q;
    
    	int byte_decimal_value = 0;
    	int weight = 128;
    	for(q = 0; q < 9; q++)
    	{
    		if(byte[q] == '1') byte_decimal_value = byte_decimal_value + weight;
    		weight = weight/2;
    	}
    
    	cout << (char) byte_decimal_value << " ";
    
    }
    
    void convertFile(char list_file[])
    {
    	int a, b;
    	int num_bytes;
    	num_bytes = strlen(list_file)/8;
    	char byte[8];
    	for(b = 0; b <= num_bytes; b++)
    	{
    		for(a = 0; a < 8; a++)
    		{
    			byte[a] = list_file[a + b*8];
    		}
    		converByte(byte);
    	}
    return;
    }

    What am I doing wrong?

  2. #2
    Join Date
    Jul 2001
    Location
    Otaki, New Zealand
    Posts
    303

    Re: Binary to ASCII converter

    I have a couple of questions which may help you get some replies

    1) Which order are the bits in the file in (Little Endian or Big Endian) ?
    2) You ask 'What am I doing wrong?', yet you provide no sample data, no expected results and nothing to indicate what results you are getting?

    Are you getting incorrect results, do you have a compiler problem, an algorithm problem or what?

    Be specific!!!

    Regards
    Alan
    Last edited by AlanGRutter; April 16th, 2007 at 08:53 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured