Results 1 to 11 of 11

Thread: decimal to binary

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    183

    decimal to binary

    humm...
    trying to convert decimal to binary but I'm a bit stuck I've been trying to do the following, but I want to assign it back to a variable, any ideas?


    void ConvertToBinary(float &line){
    int gpow=-1;
    int rem;

    //find the largest power of 2
    while( pow (2, ++gpow) <= line);

    //divide by the power of two
    while (gpow >= 0){
    line = line / pow (2, --gpow);
    cout << floor( line);
    }
    cout << endl;

    }

  2. #2
    denniswrenn
    Guest
    The function doesn't properly convert from decimal to binary. Maybe you should fix that before trying to store the binary value(once you get it fixed, I'll write up some code that stores the number in a char*).

    -D

  3. #3
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Can't you use itoa() to do that?
    Alcohol & calculus don't mix.
    Never drink & derive.

  4. #4
    denniswrenn
    Guest
    Oh.... that's right, you can

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    int main()
    {
         int dec = 5; //number to be converted
         char* pcBuf = new char[15]; //make it 15 characters long, just incase somebody puts in 32767 (15 characters long, all 1's)
         itoa(dec, pcBuf, 2); //convert int to ascii, with the base of 2
         cout << pcBuf; //outputs 101
         return(0);
    }

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Two things - make it 64 characters long in case somebody uses __int64 / longlong / long long / any other type. Second, delete[] the buffer when you've finished
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    denniswrenn
    Guest
    If they change the datatype, I guess they can change the size of the char* as well... BTW, how do you use delete[]? I tried using it a few times, but it resulted in error.... is it delete[] pcBuf; or delete pcBuf; ?

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    int *p = new int;
    int *y = new int[50];
    
    delete p;
    delete[] y;
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    denniswrenn
    Guest
    IIRC there is no need to delete the buffer since it is freed once the program ends??

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    183
    humm cool thanks that makes sense, didn't know about iota- good to know....
    and yes an array will free at the end of program execution, but what if the program runs for weeks or months- ya might need that memory again!!

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    183
    I wonder if there is a way that I can convert the array back
    into a float so I can use it outside of this method....
    any ideas?


    void ConvertToBinary(float &line){
    int power=0;

    //find the largest power of 2 that will go into the number
    while( pow (2, ++power) <= line);

    //create an array of that size
    char* pcBuf = new char[--power];

    //convert float to ascii, with the base of 2
    itoa(line, pcBuf, 2);

    cout << pcBuf << endl;
    //line = .....
    }

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You can't really convert a float to binary because you need to "represent" it in some way. Normally the IEEE sign/mantissa/exponent layout is used.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width