Thread: decimal to binary, decimal to hexadecimal and vice versa

  1. #1
    Unregistered
    Guest

    decimal to binary, decimal to hexadecimal and vice versa

    anybody knows how to convert a decimal into a binary, decimal to hexadecimal, binary to hexadecimal and decimal, hexadecimal to decimal and binary?
    can you give me the code? pls

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    anybody knows how to convert a decimal into a binary, decimal to hexadecimal, binary to hexadecimal and decimal, hexadecimal to decimal and binary?
    Yes most of us here!
    can you give me the code? pls
    No. But as you said please I'll give you some pointers. Look into sprintf() or stringstreams. The binary conversions you can do with shifts and bitwise operators.
    If you want more help than that then post your efforts.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    If you want to convert decimal to binary:

    You have a number, let's say 65.

    65 / 2 = 32 * 2 + 1
    32 / 2 = 16 * 2 + 0
    16 / 2 = 8 * 2 + 0
    8 / 2 = 4 * 2 + 0
    4 / 2 = 2 * 2 + 0
    2 / 2 = 1 * 2 + 0
    1 / 2 = 0 * 2 + 1
    0 / 2 = 0 * 2 + 0

    The result is: 01000001

    And you read the difference from below to above.

    If you want to convert binary to decimal:

    You have a number, let's say 01000001.

    0 * 2^7 + 1 * 2^6 + zero's + 1 * 2^0 = 65

    It's the same with hexadecimal and octal number system.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    9
    since the topic is up, i figured i should put my problem here. i'm working on a program that converts binary to decimal. however, my validinput(char *pbin) function isn't working properly. it's supposed to make the do while loop keep looping if the input has more than 1' and 0's.

    and then the bintodec(char *pbin) function doesn't add a one when it needs to, i have no clue why this doesn't work

    here's my code

    Code:
     #include <iostream.h>
    
     //check if input is a binary number
     int validinput(char *pbin)
     {
        for(int x = 0; x < 9; x++)
        {
           if (pbin[x] != '0' || pbin[x] != '1')
    	       return 0;
        }
    
        if (pbin[8] != '\0')
           return 0;
    
        return 1;
     }
    
     //takes the binary string and outputs a decimal number it creates
     int bintodec(char *pbin)
     {
        int base2[] = {128, 64, 32, 16, 8, 4, 2, 1};
        int num = 0;
    
        for(int x = 0; x < 8; x++)
        {
           if (pbin[x] == '1')
    	       num += base2[x];
        }
    
        return num;
     }
    
     //main part of the program
     int main()
     {
        char binary[9];
        int decimalnum;
    
        do
        {
           cout<<"Enter an 8 bit binary number: ";
           cin.getline(binary, '\n');
        }
        while(!validinput(binary));
    
        decimalnum = bintodec(binary);
        cout<<"The decimal number is: "<< decimalnum;
    
        return 0;
     }
    any help would be greatly appreciated

  5. #5
    Unregistered
    Guest
    thanks gape, ur pointer is much better than stone_coder. im just a newbie with this co'z i just started with cpp, i use vb. thanks again gape

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this:
    Code:
     //check if input is a binary number
     int validinput(char *pbin)
     {
        for(int x = 0; x < 8; x++)
        {
           if (pbin[x] != '0' && pbin[x] != '1')
    	       return 0;
        }
    
        if (pbin[8] != '\0')
           return 0;
    
        return 1;
     }
    Your bintodec() seems to work ok.

  7. #7
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Unregistered, no problem.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    9

    why does that work?

    Can somebody explain how that function that swoopy posted works? I was thinking about trying that, but i didn't because i thought it was asking if a certain character was '1' and '2' and a single character can't be both things.

    Oh yeah, thanks alot swoopy, everything works great now.

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    9

    nevermind

    i'm an idiot, i was sitting in school today and thought about it and realized why it worked.

  10. #10
    Unregistered
    Guest
    I'm a different Unregistered... Who saw a similar post on another message board,
    http://www.cpp-home.com/forums/read....&i=1454&t=1454
    see what you might glean from it if you like

Popular pages Recent additions subscribe to a feed