
-----------------------------------
Justin_
Thu Feb 16, 2006 12:10 am

Hex and Bin conversion.
-----------------------------------
Nothing special, but my first submission  :lol: 

Will continue to update and add more features to the class as I think of them and as I learn more about c++.

comments are welcome.  (if i'm doing something bad let me know.)
hex.h

#include 
#include 

class Hex
{
   private:
      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();
};



hex.cpp

#include 
#include 
#include 
#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(),hexNumber.begin(),tolower);
   for (int i = 0; i  ""
  | n -> to_bin (n / 2) ^ string_of_int (n mod 2)

-----------------------------------
Justin_
Thu Feb 16, 2006 9:57 am


-----------------------------------
It wasn't hard to make, just tedious.  I hate repetition but a solid algorithm wasn't coming to mind immediately so I decided to do it the obvious way.  Of course, that function only took less than ten minutes to make because I copied and pasted the first line and just filled in all the rest with the appropriate numbers.
For enrichment purposes I will likely design my own algorithm to make it sweeter.

-----------------------------------
Justin_
Thu Feb 16, 2006 10:58 am


-----------------------------------

Your to binary function definitely doesn't work


I believe you are definitely wrong.  I'm adding an updated version anyway, but please note I haven't changed the toBin() function.

-----------------------------------
md
Thu Feb 16, 2006 2:53 pm


-----------------------------------
Here's why it doesn't work: 0x00 is 2#00000000. Your code makes it 2#00.

a hex digit is always 4 digits of binary. Strange how wtd missed that one...

-----------------------------------
rizzix
Thu Feb 16, 2006 4:17 pm


-----------------------------------
a hex digit is always 4 digits of binary. Strange how wtd missed that one...

Not really. Well what you are talking about is bits/bytes in memory (hardware stuff). He's talking about the binary number system (math, not hardware). :)

-----------------------------------
Justin_
Thu Feb 16, 2006 4:17 pm


-----------------------------------
a hex digit can be represented with 4 digits of binary true, but a binary digit isn't always four digits.  For instance: 0 in binary is 0 in decimal.  

I can see what you are saying though, considering I used hex to convert to binary.  It's just that converting from hex to binary is much easier than converting decimal to binary.  If I knew of an easier way I would have used it.

-----------------------------------
avok23
Mon May 05, 2008 8:44 pm


-----------------------------------
Your to binary function definitely doesn't work ;)

And why is this a class? It's much better written as a couple of functions...

i think he was trying to create an interface like the type you get in managed languages c# and java

Math.Sin(  angle ); just in case u have a function with that name. i would probably use a namespace instead
