Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Hex and Bin conversion.
Index -> Programming, C++ -> C++ Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Justin_




PostPosted: Thu Feb 16, 2006 12:10 am   Post subject: Hex and Bin conversion.

Nothing special, but my first submission Laughing

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
c++:

#include <string>
#include <iostream>

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
c++:

#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(),hexNumber.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;
}


Example:
c++:

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

int main ()
{
    Hex number(255);
    std::cout << number.toHex() << "\n";
    std::cout << number.toInt() << "\n";
    std::cout << number.toBin() << "\n";
    return 0;
}
Sponsor
Sponsor
Sponsor
sponsor
md




PostPosted: Thu Feb 16, 2006 12:13 am   Post subject: (No subject)

Your to binary function definitely doesn't work Wink

And why is this a class? It's much better written as a couple of functions...
Justin_




PostPosted: Thu Feb 16, 2006 12:24 am   Post subject: (No subject)

how doesn't it work? And in the near future you'll see why its in a class Wink
wtd




PostPosted: Thu Feb 16, 2006 1:07 am   Post subject: (No subject)

Wow... that binary conversion function is excruciatingly complex for no good reason. Now, I won't give you the code outright in C++, but I will show it to you in O'Caml. Wink

code:
let rec to_bin =
  function
  | 0 -> ""
  | n -> to_bin (n / 2) ^ string_of_int (n mod 2)
Justin_




PostPosted: Thu Feb 16, 2006 9:57 am   Post subject: (No subject)

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_




PostPosted: Thu Feb 16, 2006 10:58 am   Post subject: (No subject)

Cornflake wrote:

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




PostPosted: Thu Feb 16, 2006 2:53 pm   Post subject: (No subject)

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




PostPosted: Thu Feb 16, 2006 4:17 pm   Post subject: (No subject)

Cornflake wrote:
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). Smile
Sponsor
Sponsor
Sponsor
sponsor
Justin_




PostPosted: Thu Feb 16, 2006 4:17 pm   Post subject: (No subject)

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




PostPosted: Mon May 05, 2008 8:44 pm   Post subject: (No subject)

md @ Thu Feb 16, 2006 12:13 am wrote:
Your to binary function definitely doesn't work Wink

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
Display posts from previous:   
   Index -> Programming, C++ -> C++ Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: