Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

[SOURCE] C++ simple binary converter EXTREMELY noob friendly

haven't done any c++ in a while so i made this little program for fun. If you're new to c++ look closely at the pointers, stucts, and functions.

Code
//
//  main.cpp
//  Binary Converter
//
//  Created by r3m3mb3r on 7/9/14.
//  Copyright (c) 2014 r3m3mb3r. All rights reserved.
//
#include <string>
#include <iostream>
#include <queue>

using namespace std;


////////////////////////////////////////////////////////////////////////////////////
// byte struct
// simulates a byte for printing and storing purposes
// for the newbies it basically looks like this {0,0,0,0,0,0,0,0}
// and each place can be a 0 or 1. also a byte is 8bits, and so is a char
// also has overloaded << operator for printing
struct byte {
    int bit[8] = {};
    
    friend ostream& operator << (ostream& os, const byte &rhs ){
        for(int i = 0; i < 8; i++)
            os << rhs.bit[i];
        return os;
    }
};
////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////
// decToBin function
// converts decimal number to binary number
// a character can be casted as an int so we take that number and turn it to binary
// the byte that we are working on is passed in and so is the current character
void decToBin(int num, byte *curByte)
{
    int remainder = 0, place = 7;
    
    while(num!=0)
    {
        remainder = num % 2;
        num = num / 2;
        curByte->bit[place] = remainder;
        place--;
        
    }
}
////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////
// binToDec function
// converts decimal numbers to binary
// passes in the current byte being worked on
// returns the number in decimal
int binToDec(byte curByte)
{
    int bitPlace = 7;
    int num = 0;
    //starts at rhs of bit and works way left
    for(int i = 0; i < 8; i++){
        if(i==0)
            num = num + (curByte.bit[bitPlace]);
        else
            num = num + (curByte.bit[bitPlace]*(2<<(i-1)));
        bitPlace--;
        
    }
  
    return num;
    
}
////////////////////////////////////////////////////////////////////////////////////



//function declaration
//newbies note use of pointers in functions
void decToBin(int num, byte *curByte);
int binToDec(byte curByte);


//main
int main(int argc, const char * argv[])
{

    byte current;  // current "byte"
    string str;    // String from user
    char c;        // placeholder to keep window open
    int choice = -1; //holds users choice to encode/decode
    int currentChar = 0; // current character being converted
    char *inPut;       // c_array of users input
    queue<byte> binStr; //a queue used to store all the bytes dynamically
    
    while(choice != 0){
        cout << "\nEnter 0 to quit\nEnter 1 for binary to decimal\nEnter 2 for decimal to binary\n";
        cin >> choice;
    
        if(choice == 1){
            cout << "Please enter binary number to be decoded"
                 << str.max_size() << " characters \n";
        
            //gets users data
            cin.ignore();
            getline(cin, str);
        
            //converts string to c_string for dynamic enteries
            inPut = new char[str.length()+1];
            strcpy(inPut, str.c_str());
        
            for(int i = 0; i < str.size()+1; i++)
            {
                //puts user input into byte struct
                if(inPut[i] == '0')
                    current.bit[i%8] = 0;
                else if(inPut[i] == '1')
                    current.bit[i%8] = 1;
            
                //inserts byte to que after 8 bits
                if((i%8 == 0)&&(i>0)){
                    binStr.push(current);
                    //resets current byte
                    for(int i = 0; i < 8; i++)
                        current.bit[i] = 0;
                }
            
            }
            //prints the queue
            while(!binStr.empty()){
                cout << (char)binToDec(binStr.front());
                binStr.pop();
            }
        }
        else if(choice == 2){
            cout << "Please enter text to be encoded max size is "
                 << str.max_size() << " characters \n";
    
            //gets users data
            cin.ignore();
            getline(cin, str);
        
            //converts string to c_string for dynamic enteries
            inPut = new char[str.length()+1];
            strcpy(inPut, str.c_str());
    
            //encodes to binary then adds bytes to stack
            for(int i = 0; i < str.size(); i++)
            {
                //converts char to int
                currentChar = (int)inPut[i];
                //converts int to binary
                decToBin(currentChar, &current);
                //adds current byte to queue
                binStr.push(current);
                //resets current byte
                for(int i = 0; i < 8; i++)
                    current.bit[i] = 0;
        
            }
    
            //prints the queue
            while(!binStr.empty()){
                cout << binStr.front();
                binStr.pop();
            }
        }
    }
    
    //holds the window open
    cin >> c;
    
    return 0;
}
I'd highly recommend using std::bitset in the future - it's optimized to do exactly this.
(Jul 23, 2014 09:58 AM)ctrlaltdestroy_ Wrote:  I'd highly recommend using std::bitset in the future - it's optimized to do exactly this.

thanks for the reply, ill look into it nver used it before


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by Robert RougeSep 24, 2026 09:58 AM 4 275 Sep 24, 2026 09:58 AM
Last Post: Anonynuk
[Python] by SIM-2362662Jan 12, 2020 05:30 PM 5 216 Jan 12, 2020 05:30 PM
Last Post: Riԁdle
[.NET] by ZeakZNov 17, 2018 09:53 AM 22 819 Nov 17, 2018 09:53 AM
Last Post: ZeakZ