Binary to Hexa converter c++ HELP

This is a discussion on Binary to Hexa converter c++ HELP within the C++ Programming forums, part of the General Programming Boards category; #include <iostream> #include <string> #include <sstream> #include "hex.h" //constructor Hex::~Hex() //destructor { } int Hex::toInt() { int num; std::stringstream toBeConverted(hexNumber); ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    1

    Binary to Hexa converter c++ HELP

    #include <iostream>
    #include <string>
    #include <sstream>
    #include "hex.h"

    //constructor

    Hex::~Hex() //destructor
    {
    }

    int Hex::toInt()
    {
    int num;
    std::stringstream toBeConverted(hexNumber);
    toBeConverted >> std::hex >> num;
    return num;
    }

    std::string Hex::toBin()
    {
    std::string binaryNum;
    std::transform(hexNumber.begin(),hexNumber.end(),h exNumber.begin(),tolower);
    for (int i = 0; i <= hexNumber.length() ; i++)
    {
    if (hexNumber.substr(i,1) == "0")
    binaryNum += "0";
    if (hexNumber.substr(i,1) == "1")
    binaryNum += "1";
    if (hexNumber.substr(i,1) == "2")
    binaryNum += "10";
    if (hexNumber.substr(i,1) == "3")
    binaryNum += "11";
    if (hexNumber.substr(i,1) == "4")
    binaryNum += "100";
    if (hexNumber.substr(i,1) == "5")
    binaryNum += "101";
    if (hexNumber.substr(i,1) == "6")
    binaryNum += "110";
    if (hexNumber.substr(i,1) == "7")
    binaryNum += "111";
    if (hexNumber.substr(i,1) == "8")
    binaryNum += "1000";
    if (hexNumber.substr(i,1) == "9")
    binaryNum += "1001";
    if (hexNumber.substr(i,1) == "a")
    binaryNum += "1010";
    if (hexNumber.substr(i,1) == "b")
    binaryNum += "1011";
    if (hexNumber.substr(i,1) == "c")
    binaryNum += "1100";
    if (hexNumber.substr(i,1) == "d")
    binaryNum += "1101";
    if (hexNumber.substr(i,1) == "e")
    binaryNum += "1110";
    if (hexNumber.substr(i,1) == "f")
    binaryNum += "1111";
    }
    return binaryNum;
    }

    std::string Hex::toHex()
    {
    std::stringstream hexNum;
    hexNum << std::hex << decNumber;
    hexNum >> hexNumber;
    return hexNumber;
    }


    i need a converter that will accept a input of a binary then it will convert it to hexa.. if you have a better way or code pls post it here i really need your help guys please... T_T.. im really noob at programming i wanna disect this program so i can defend it to my prof.. thanks in future be patient waiting ..

  2. #2
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    First, indent your code.
    Second, put your code in tags.
    Finally, tell, where the problem is, we can't read the whole program sometimes... Tell us what problem you got.....

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Why even uses classes here? That's an extreme overkill imho. Why don't you just use the function "strtol". It does all that and more, in just one standard function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 03-06-2011, 12:24 PM
  2. Decimal to Binary Converter
    By peckitt99 in forum C Programming
    Replies: 16
    Last Post: 10-12-2006, 06:25 AM
  3. 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
  4. Help with Error - Binary to Character Converter
    By dvldrmmr in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 02:21 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