Thread: Decimals To Binary

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    208

    Decimals To Binary

    I am having quite a bit of truble finding a tutorial on the net about how to convert a ASCII deciaml into binary format for encryption purposes. I have found plenty about converting it to the actual decimals but i want to take those deciamls and turn them into binary. Can anyone out there help me or point me into the direction to some help. Thanx

    Kas2002
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    317
    The easiest way to do it is to break it down into steps. Start by converting the decimal value to its hexadecimal equivalent, then convert the hex value to binary. The coding for these individual steps is not too challenging, just think about it.

  3. #3
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    Taken from www.cpp-home.com

    Code:
    #include <iostream.h> 
    
    int a,b; 
    int n=0; 
    int arr[100]; 
    
    void main() 
    { 
    
        cout <<"Decimal : "; 
        cin >>a; 
      
        while (a>0) 
        { 
            n++; 
            b=a/2; 
        
            if (a%2==0) 
            { 
                arr[n]=0; 
            } 
            else 
            { 
                arr[n]=a-(2*b); 
            } 
        
            cout <<"b("<<n<<") = "<< b<<"       "; 
            a=b; 
            cout <<"arr["<<n<<"] = "<< arr[n]<<endl; 
        } 
      
        cout<<"Binary : "; 
        for (n=n;n>0;n--) 
        { 
            cout <<arr[n]; 
        } 
    }
    the best things in life are simple.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421

    Lightbulb

    if you are looking to simply output them, then give this a go:

    Code:
    #include <iostream>
    using namespace std;
    
    void OutputInBinary(int item)
    {
        for(int i = 0; i < sizeof(int) * 8; i++)
        {
            cout << (((item << i) & (0x1 << (sizeof(int) * 8 - 1))) ? 1 : 0);
        }
    }
    
    int main(void)
    {
        OutputInBinary(1023);
        cout << endl;
        OutputInBinary(12345);
        cout << endl;
        OutputInBinary(-4832);
        cout << endl;
        return(1);
    }
    if you're just going to be using it for encryption purposes then you dont need to 'convert' to binary, coz it's already stored in binary form.

    if the above sample doesn't solve your problem, can you give more information?

    cheers
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  5. #5
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Most binary logic is done in unsigned chars. And it can almost always be safely assumed that an unsigned char is 8 bits.

    This often makes it eaisiar to work with ASCII characters in binary. But remember this, if you are doing this for encryption purposes, then if you perform bitwise manipulations on a character such than every character corresponds to one and only one other possible value of an unsigned char, then all you are doing is making a cryptogram.

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    208

    This Is What I want to

    I found a 64 bit encryption tutorial at cpp-home.com and in the tutorial it calls for you to change the txt into decimals and then cahnge the decimals into binary so u can do the next step. They didn't say how to do this because they said there is a tutorial on cpp-home.com that says how to do this and I have been looking and never found one. I haven't been able to contiue witht he tutorial because I have no idea how to change the decimal to binary and store it into an array for modification? If someone could just tell me the formula or process u got to go through in English Icould prolly code it my self- it would be much better that way then I would understand my algorithm.
    Last edited by kas2002; 06-07-2002 at 12:22 PM.

  7. #7
    Unregistered
    Guest

    my stuff

    when i'm dealing with ciphering, i use some functions i made.

    /*
    theINT is the integer of a number system that you are converting from.
    theBASE is the base integer of the number system that you are converting to.
    MaxExp defines the explicit maximum value of a possible divisor.

    Example function calling:

    Convert 2277 to decimal using the functions in the hmain.h header
    file.

    int RetVal = getRange(2227, 2, 32);

    int *bMEM;

    bMEM = new int[RetVal];

    bMEM = n2bin(2227, RetVal, pow(2, RetVal));

    Use a loop to display the array bMEM.


    */
    int getRange(int theINT, int theBASE, int MaxExp)
    {
    for(int r = 0; r <= MaxExp; r++)
    {
    if(pow(theBASE, r) > theINT)
    {
    return r;
    }
    }

    return -1;
    }



    int *n2bin(int theINT, int range)
    {
    int y = 0, bdex = 0, *bbank;
    bbank = new int[range];

    long d = (long)pow(2, range);

    y = theINT;

    while(d > 1)
    {
    d /= 2;

    if((y / d) == 1)
    {
    y %= d;
    bbank[bdex++] = 1;
    } else {
    bbank[bdex++] = 0;
    }
    }
    return bbank;
    }

    int bin2n(int bin[])
    {
    int d, z = 0, x;

    for(int n = 0; n <= 32; n++)
    {
    if((bin[n] > 1 || bin[n] < 0))
    {
    x = (n - 1);
    d = (int)pow(2, n);
    break;
    }
    }

    for(int r = 0; r <= x; r++)
    {
    d /= 2;

    z += (bin[r] * d);
    }

    return z;
    }

  8. #8
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    umm...kas2002, please look at my post above.
    the best things in life are simple.

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    208

    Yes but

    I to be able to store the binary for later manipulation instead of just couting it one at a time. Plus it would be nice to know the process I would have to go through instead of someone just giving it to me????
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 PM
  2. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  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. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 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