Converting string/character to binary?

This is a discussion on Converting string/character to binary? within the C++ Programming forums, part of the General Programming Boards category; Hello everyone. I am a programmer in the C/C++ language and many web languages for a few years. And I ...

  1. #1
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Question Converting string/character to binary?

    Hello everyone. I am a programmer in the C/C++ language and many web languages for a few years. And I am 16-years-old. I was just wanting to make a program that will convert and print a user-specified string into binary (1's and 0's). It's just something I would like to do for fun. Would I need to convert each character to binary form? If so, how?

    Thanks in Advance,
    Matt U.
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: trifaze_mattu@lycos.com

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Convert each character to it's ASCII code. That's real easy. Just cast your char to int. Then, convert that integer in base ten to a binary number.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Question How would I go about doing so?

    Can you please tell me how I would do that. Sorry, I've just never done anything with converting to binary, etc. :P
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: trifaze_mattu@lycos.com

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  4. #4
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Iīve made this code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
       /*(4 bytes of an integer)x(8 bits of a byte)*/ 
       const int mySize = sizeof(int)*4;
       cout << "Enter a number " << endl;
       int number;
       cin >> number;
       char bin_number[mySize];
       char compare=1; /* just to set a number with 00...001 */
       for(int i=0;i<mySize;i++){
          /* compare the last bit of the current number, to see if itīs 1*/
          if( (number&compare)==compare ) bin_number[mySize-1-i]='1';
          else bin_number[mySize-1-i]='0';
          number>>=1; /* shift the number one bit rigth */
       }
       for(int i=0;i<mySize;i++){
          cout << bin_number[i];
       }
       cout << endl;
       return 0;
    }
    Hope that helps!
    Nothing more to tell about me...
    Happy day =)

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Do a search of this board or the internet, and you'll find a number of ways to convert from base ten to binary. Here's what I think is the easiest way to do it and the way I was recently taught in my Intro to CS course:
    http://www.mcs.drexel.edu/~introcs/F...CurrentSlide=4
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Very easy to do using a bitset, a few lines of code is all...

    Code:
    #include <bitset>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string data("Hello there.");
    
        for( string::iterator it = data.begin(); it != data.end(); ++it )
            cout << bitset<8>((unsigned long)*it) << endl;
    
        return 0;
    }
    Baring any typos, this outputs:
    Code:
    01001000
    01100101
    01101100
    01101100
    01101111
    00100000
    01110100
    01101000
    01100101
    01110010
    01100101
    00101110
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 07:17 PM
  2. Converting From Binary Tree to Threaded Binary Trees
    By elton_fan in forum C Programming
    Replies: 15
    Last Post: 11-08-2007, 11:41 PM
  3. Converting digit to binary using the POW function
    By STDSkillz in forum C Programming
    Replies: 6
    Last Post: 10-14-2007, 10:43 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21