?
Solved

converting a text to binary

Posted on 2003-03-24
8
Medium Priority
?
430 Views
Last Modified: 2013-11-15
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
Comment
Question by:stallion5000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 1

Expert Comment

by:oferh
ID: 8198637
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
 
LVL 1

Expert Comment

by:MattRobb
ID: 8198773
// 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
 
LVL 49

Accepted Solution

by:
DanRollins earned 2000 total points
ID: 8199219
#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!

 
LVL 49

Expert Comment

by:DanRollins
ID: 8199306
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
 
LVL 22

Expert Comment

by:ambience
ID: 8200327
>> 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
 
LVL 49

Expert Comment

by:DanRollins
ID: 8200820
I was just checking to see if you'd notice :)
-- Dan
0
 

Author Comment

by:stallion5000
ID: 8219277
Excellent answer and great turn around, but a bit to complicated for what I need.
Thanks
G
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 8219907
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

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many software programs on offer that will claim to magically speed up your computer. The best advice I can give you is to avoid them like the plague, because they will often cause far more problems than they solve. Try some of these "do it…
Ever wonder what it's like to get hit by ransomware? "Tom" gives you all the dirty details first-hand – and conveys the hard lessons his company learned in the aftermath.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question