Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Hex and Bin conversion.
Author Message
Justin_

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

Nothing special, but my first submission

Will continue to update and add more features to the class as I think of them and as I learn more about c++.

hex.h
 c++: #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
 c++: #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 <= 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 #include #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; }

md

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

Your to binary function definitely doesn't work

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

Posted: 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
wtd

Posted: 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.

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

Posted: 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_

Posted: 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

Posted: 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

Posted: 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).

Justin_

Posted: 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

Posted: 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

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: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 10 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: