This is a discussion on How can i convert integers to binary or hexadecimal? within the C and C++ Programming forums, part of the Software Development category; Hi, can anyone tell how can i convert integers to binary or hexadecimal,plz give one example program. Thanks, prasath....
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Defines a class for representing 16-bit binary numbers as sequences of 0 and 1 characters. #include <iostream.h> #include <string.h> int const binSize = 16; class Binary { public: Binary (const char*); Binary (unsigned int); friend Binary operator + (const Binary, const Binary); operator int (); // type conversion void Print (void); private: char bits[binSize]; // binary quantity }; Annotation 6 This constructor produces a binary number from its bit pattern. 7 This constructor converts a positive integer to its equivalent binary representation. 8 The + operator is overloaded for adding two binary numbers. Addition is done bit by bit. For simplicity, no attempt is made to detect overflows. 9 This type conversion operator is used to convert a Binary object to an int object. 10 This function simply prints the bit pattern of a binary number. 12 This array is used to hold the 0 and 1 bits of the 16-bit quantity as characters. The implementation of these functions is as follows: Binary::Binary (const char *num) { int iSrc = strlen(num) - 1; int iDest = binSize - 1; while (iSrc >= 0 && iDest >= 0) // copy bits bits[iDest--] = (num[iSrc--] == '0' ? '0' : '1'); while (iDest >= 0) // pad left with zeros bits[iDest--] = '0'; } Binary::Binary (unsigned int num) { for (register i = binSize - 1; i >= 0; --i) { bits[i] = (num % 2 == 0 ? '0' : '1'); num >>= 1; } } Binary operator + (const Binary n1, const Binary n2) { unsigned carry = 0; unsigned value; Binary res = "0"; for (register i = binSize - 1; i >= 0; --i) { value = (n1.bits[i] == '0' ? 0 : 1) + (n2.bits[i] == '0' ? 0 : 1) + carry; res.bits[i] = (value % 2 == 0 ? '0' : '1'); carry = value >> 1; } return res; } Binary: perator int () { unsigned value = 0; for (register i = 0; i < binSize; ++i) value = (value << 1) + (bits[i] == '0' ? 0 : 1); return value; } void Binary::Print (void) { char str[binSize + 1]; strncpy(str, bits, binSize); str[binSize] = '\0'; cout << str << '\n'; } The following main function creates two objects of type Binary and tests the + operator. main () { Binary n1 = "01011"; Binary n2 = "11010"; n1.Print(); n2.Print(); (n1 + n2).Print(); cout << n1 + Binary(5) << '\n'; // add and then convert to int cout << n1 - 5 << '\n'; // convert n2 to int and then subtract } The last two lines of main behave completely differently. The first of these converts 5 to Binary, does the addition, and then converts the Binary result to int, before sending it to cout. This is equivalent to: cout << (int) Binary: perator+(n2,Binary(5)) << '\n'; The second converts n1 to int (because - is not defined for Binary), performs the subtraction, and then send the result to cout. This is equivalent to: cout << ((int) n2) - 5 << '\n'; In either case, the user-defined type conversion operator is applied implicitly. The output produced by the program is evidence that the conversions are performed correctly: 0000000000001011 0000000000011010 0000000000100101 16 6 |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| What is Binary Portability Testing? | simplesabita | Software Testing | 1 | 11-13-2007 04:36 AM |
| How to convert Color to it's corresponding Hexadecimal value? | kingmaker | ASP and ASP.NET Programming | 1 | 09-28-2007 11:40 PM |
| Development using Binary? | prasannavigneshr | Technology BUZZzzzzz | 12 | 08-06-2007 10:11 AM |
| How to convert hexadecimal value to rgb value in flash? | kingmaker | Flash Actionscript Programming | 1 | 07-24-2007 06:01 AM |
| How to convert Decimal to Binary in Flash? | kingmaker | Flash Actionscript Programming | 1 | 07-21-2007 02:48 AM |