• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 438
  • Last Modified:

converting a text to binary

This is for a C++ program. A text file will be read in. ANd for example the text will say

"Provide details on your quesiton. Please be as precise and clear as possible. Don't worry about giving too much information."

And what I need is a formula or the part of the program in the loop  that will convert that text into binary, its corresponding 1 and 0's. And then cout all of it on the screen.
0
stallion5000
Asked:
stallion5000
1 Solution
 
oferhCommented:
char str[] = "string to bin";

for (int i=0; str[i] != '\0';i++)
{
  for(j=0;j<8;j++)
  {
    char tmp = str[i];
    if (tmp & 1) cout <<"1";
    else cout <<"0";
    tmp >>= 1;
  }
}
0
 
MattRobbCommented:
// 15.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream.h>
#include <string.h>

void CharToBinary(char Char, char* buffer);

int main(int argc, char* argv[])
{
     char     acBin[9];
     char     *lpstrSampleText = "sample text";
     int          cSampleTextLenght = strlen(lpstrSampleText);

     for(char i=0; i<cSampleTextLenght; i++){
          CharToBinary(lpstrSampleText[i], acBin);
          cout << acBin << endl;
     }

     return 0;
}


//buffer's length is at least 9 chars
//Char - charater to convert
//buf - pointer to buffer that receives binary form of the Char
void CharToBinary(char Char, char* buf){
     char     mask = 1;
     for(int i = 0; i < 8; i++){
          buf[7 - i] = (Char & mask) ? '1' : '0';
          mask <<=1;
     }
     buf[8] = 0;
}
0
 
DanRollinsCommented:
#include<iostream>
#include<fstream>
using namespace std;
const char* AsBinary( unsigned char b )
{
     static char szRet[9];
     strcpy( szRet,"00000000");
     char szTmp[9];
     itoa( b,szTmp,2 );
     strcpy( &szRet[ 8-strlen(szTmp) ], szTmp );
     return( szRet );
}

void main(void)
{
     unsigned char b;    
     ifstream fIn;
     fIn.open("c:\\temp\\thetext.txt", ios_base::binary | ios_base::in );
     while (! fIn.eof() ) {
          fIn >> b;
          cout << AsBinary(b) << " ";
     }
     cout << endl;
}

Input:
Provide details on your quesiton. Please be as precise and clear as possible. Don't worry about giving too much information.

Outnput:
01010000 01110010 01101111 01110110 01101001 01100100 01100101 01100100 01100101
 01110100 01100001 01101001 01101100 01110011 01101111 01101110 01111001 0110111
1 01110101 01110010 01110001 01110101 01100101 01110011 01101001 01110100 011011
11 01101110 00101110 01010000 01101100 01100101 01100001 01110011 01100101 01100
010 01100101 01100001 01110011 01110000 01110010 01100101 01100011 01101001 0111
0011 01100101 01100001 01101110 01100100 01100011 01101100 01100101 01100001 011
10010 01100001 01110011 01110000 01101111 01110011 01110011 01101001 01100010 01
101100 01100101 00101110 01000100 01101111 01101110 00100111 01110100 01110111 0
1101111 01110010 01110010 01111001 01100001 01100010 01101111 01110101 01110100
01100111 01101001 01110110 01101001 01101110 01100111 01110100 01101111 01101111
 01101101 01110101 01100011 01101000 01101001 01101110 01100110 01101111 0111001
0 01101101 01100001 01110100 01101001 01101111 01101110 00101110 00101110
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
DanRollinsCommented:
In my code above, the AsBinary function uses the itoa libary function to generate a character string which is a representation of the binary value of the input byte, in radix 2.  Binary output must be in groups of 8 digits, but the atoi() fn can return anywhere between 1 and 8 characters, so there is a piece of code in the AsBinary() fn where I transfer the digits into a buffer that has been pre-filled with zeros (ASCII '0', 0x31).

I have created the AsBinary function because it helps me to break out the logic for the conversion from the logic of the input and output operations.  For simplicy of operation, it actually maintains an static buffer and just returns a pointer to that buffer.  I would not use this technique in a multithreaded program becasue that local buffer might be corrupted if two threads were calling AsBinary at the same time.  But it is a perfectly valid technique in this sort of situation.

The main function uses C++ STL file classes for reading the text from the file, one character at a time.  For output, I insert a single space between each 8-character numeric value to show that leading zeros are included.  

The program could be made more efficent by reading the entire file into memory and then processing the bytes, but that would complicate the logic flow slightly and the efficiency level was not sepecified in the assignment sheet.

-- Dan
0
 
ambienceCommented:
>> The main function uses C++ STL file classes for reading the text from the file

wow, so you are begining to appreciate STL, finally !!
0
 
DanRollinsCommented:
I was just checking to see if you'd notice :)
-- Dan
0
 
stallion5000Author Commented:
Excellent answer and great turn around, but a bit to complicated for what I need.
Thanks
G
0
 
DanRollinsCommented:
Thanks for accepting my answer.

>>but a bit to complicated for what I need.

Does that mean that the professor would never believe for even one second that you wrote it yourself?   To "dumb it down" just add a few typographical errors.  For instance, replace all of tmy code with:

        voodoo mane()
        {
                prmfp(Dang I wush i new all this stuff, to),
        }

I think the prof will buy that because it's hardly complicated at all.  
-- Dan
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now