Need help converting from hex to binary any help is very appreciated!!

#include <string> 
#include <iostream> 

class Hex 
{ 
   private:						//constructor 
      std::string hexNumber; 
      int decNumber; 
   public: 
      Hex(std::string hNum): hexNumber(hNum) 
      {} 
      Hex(int dNum): decNumber(dNum) 
      {} 
      ~Hex(); 
      int toInt(); 
      std::string toBin(); 
      std::string toHex(); 
};

it keeps saying i need to put a semicolon before the period on line 23 not too sure exactly where its talking about or maybe i did something wrong.

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

                       

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::string hexNumber.begin(),hexNumber.end(),hexNumber.begin(),tolower;
   for (int i = 0; i = hexNumber.length() ; i++) 
   { 
      if (hexNumber.substr(i,1) == "0") 
         binaryNum  += "0000"; 
      if (hexNumber.substr(i,1) == "1") 
         binaryNum  += "0001"; 
      if (hexNumber.substr(i,1) == "2") 
         binaryNum  += "0010"; 
      if (hexNumber.substr(i,1) == "3") 
         binaryNum  += "0011"; 
      if (hexNumber.substr(i,1) == "4") 
         binaryNum  += "0100"; 
      if (hexNumber.substr(i,1) == "5") 
         binaryNum  += "0101"; 
      if (hexNumber.substr(i,1) == "6") 
         binaryNum  += "0110"; 
      if (hexNumber.substr(i,1) == "7") 
         binaryNum  += "0111"; 
      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; 
}

The compiler can't figure out what you are trying to do (nor can I), so it's throwing up its hands and giving up and throwing an error.

You have three options when declaring a variable (there may be more I am thinking of)...

std::string hexNumber;
std::string hexNumber(/* some expression that matched a constructor */);
std::string hexNumber = /* something */;

So your new variable name needs to be followed by either parentheses, a semicolon, or an equals sign (can't think of anything else legal). Yours is followed by a dot, which would be fine if it had ALREADY been declared and didn't have the std::string in front of it.

So I think you're trying to declare variables and use them all in one stop. You need to declare them, possibly initialize them, THEN use them with the "." to call functions.

But the syntax is messed up in lines 23 and 24. You are clearly trying to iterate through a string, presumably hexNumber, but that means you shouldn't be declaring hexNumber in line 23 since it's already a class variable. You need to get rid of the std::hexNumber altogether in line 23. You already have it in line 7.

>> You need to get rid of the std::hexNumber altogether in line 23.

Sorry. Obviously there's no std::hexNumber. I meant std::string.

oh ok... i thought so but someone told me i needed that there for some reason... i took that line out and now it says i have a link error

>> i took that line out and now it says i have a link error

You took out line 23 and got a linker error? I'm not seeing any obvious ones. Line 24 is wrong, but it should still link as far as I can tell. What's the error?

the [.length()] you mean? but if i remove it then i get an error since you cant go from sting to int.... it says "LNK1561: entry point must be defined"

>> it says "LNK1561: entry point must be defined"

That has nothing to do with the code you posted. Sounds like you're compiling it incorrectly. The files don't have a main. If you have a main somewhere else, it's not finding it.

>> the [.length()] you mean?

No. I mean for loops are supposed to look like this:

for(int i = /* initialize */; /* some true/false comparison */; /* some change in i */)

You have an assignment in the middle (=), not a comparsion. Comparisons are <. >, ==, >=, <=, etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.