Thread: binary converter

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    465

    binary converter

    I know how I would convert each string of binary seperately, but not all at once. Same thing for text. When it is converted to text my program will have no way of telling how the characters fit together.
    My computer is awesome.

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    What exactly are you trying to do? I can't figure it out from your post. Are you trying to convert 0s and 1s to the appropriate ASCII values? If so, all of the ASCII values will be the same length - 8 bits.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    I'm trying to convert text to binary and binary to text.
    My computer is awesome.

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    I'd use unsigned chars and the & operator. Please excuse the sloppy code.
    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
        int format = 0;
        char* raw_input = new char[100];
        unsigned char* input = new unsigned char[100];
        cin.getline(raw_input, 100);
        for(int z = 0; ; z++)
        {
                if(raw_input[z] == '\0')
                {
                 input[z] = '\0';
                 break;
                }
                input[z] = (unsigned char)raw_input[z];
        }
        for(int x = 0; ;x++)
        {
        if(input[x] == '\0') break;
         for(unsigned char y = (unsigned char)1; ; y *= (unsigned char)2)
         {
                  if(input[x] & y) cout << "1";
                  else cout << "0";
                  if(y == 128) break;
         }
         cout << " ";
         format++;
         if(format > 5)
         {
         format = 0;
         cout << endl;
         }
        } 
        system("PAUSE");
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Question File I/O?

    Quote Originally Posted by cerin
    I'm trying to convert text to binary and binary to text.
    Are you talking about File I/O? i.e. Saving text in "binary" format? Or, reading an ASCII file in "binary" format? This has nothing to do with base 2, or ones & zeros.

    Or, do you want to type an 'A', and then display "01000001"?

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Text files are binary files following certain standard notations.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal to Binary Converter
    By peckitt99 in forum C Programming
    Replies: 16
    Last Post: 10-12-2006, 05:25 AM
  2. Binary Converter - Various stupid string issues :p
    By polarpete in forum C++ Programming
    Replies: 7
    Last Post: 08-21-2005, 02:46 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Help with Error - Binary to Character Converter
    By dvldrmmr in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 01:21 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM