Turn your Mobile Apps into m-commerce apps – Learn More!

You're Browsing As A Guest! Register Now...
Become a C++ Expert!

Join 416,727 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,950 people online right now.Registration is fast and FREE... Join Now!



  • (2 Pages)
  • +
  • 1
  • 2

Decimal to Hexadecimal Rate Topic: -----

#1 Umbrella  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 13-October 06


Dream Kudos: 0

Share |

Decimal to Hexadecimal

Posted 15 October 2006 - 03:47 PM

How can i convert decimal to hexadecimal in c++?
The number can be any number.
Was This Post Helpful? 1


#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • Icon

Reputation: 202
  • View blog
  • Posts: 13,456
  • Joined: 12-July 02


Dream Kudos: 25

Re: Decimal to Hexadecimal

Posted 15 October 2006 - 06:03 PM

There are a few ways...do you actually need it in a variable in it's changed form?

You can use the sprintf() function

http://www.cplusplus...io/sprintf.html

, or set a base format flag.

http://www.cplusplus...ec_hex_oct.html

Alternately, you can implement a conversion function of your own.
Was This Post Helpful? 0
  • +
  • -

#3 Umbrella  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 13-October 06


Dream Kudos: 0

Re: Decimal to Hexadecimal

Posted 15 October 2006 - 06:14 PM

yea we haft to make are own algorithm that will convert a decimal into a hexadecimal.

cant figure that part out..
Was This Post Helpful? 0
  • +
  • -

#4 NyeNye  Icon User is offline

  • D.I.C Head
  • PipPip

Reputation: 1
  • View blog
  • Posts: 248
  • Joined: 24-September 06


Dream Kudos: 0

Re: Decimal to Hexadecimal

Posted 15 October 2006 - 06:27 PM

well make a template for that.....
Was This Post Helpful? 0
  • +
  • -

#5 bluesuus  Icon User is offline

  • D.I.C Head
  • Icon

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 26-December 05


Dream Kudos: 25

Re: Decimal to Hexadecimal

Posted 16 October 2006 - 12:09 AM

Well, umbrella.if i understand problem.the i tnk this is wt ur professor wants.how do u convert from one base to another on paper to do is successive division and modulo.

take for e.g you want to convert 16 in base 10 to hexadeciaml number.

16/16 =1 remaider 0;

1/16 = 0 remainder 1

so ur anser is 10 in base 16....reading from bottom.

an appropriate c++ snippet can be.
while( number != 0 )
{
a[i] = number % 16;
number = number/16;
}

note: that u hv to put ur remainders in an array because, so that you can print the remainders from bottom

EDIT : Added Code Tags - born2c0de
Was This Post Helpful? 0
  • +
  • -

#6 Umbrella  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 13-October 06


Dream Kudos: 0

Re: Decimal to Hexadecimal

Posted 16 October 2006 - 01:23 AM

ah wish i checked back i eventually figured it out and did it exactly like that . Thank you though!!
Was This Post Helpful? 0
  • +
  • -

#7 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • Icon

Reputation: 143
  • View blog
  • Posts: 4,636
  • Joined: 26-November 04


Dream Kudos: 2825

Expert In: J2ME, 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

Re: Decimal to Hexadecimal

Posted 16 October 2006 - 03:53 AM

Quote

a[i] = number % 16;


Instead of changing 10 to 15 to A to F later in the code, we can implement it directly like this:
a[i] = (number%16) < 10 ? (number%16) : 'A' + (number%16) - 10;


Was This Post Helpful? 0
  • +
  • -

#8 michaelangelo  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-April 08


Dream Kudos: 0

Re: Decimal to Hexadecimal

Posted 02 April 2008 - 08:41 PM

View Postborn2c0de, on 16 Oct, 2006 - 04:53 AM, said:

Quote

a[i] = number % 16;


Instead of changing 10 to 15 to A to F later in the code, we can implement it directly like this:
a[i] = (number%16) < 10 ? (number%16) : 'A' + (number%16) - 10;




so if that number is bigger than 10 it can be print as a hexadecimal with this code

for example

cout<<number;

and answer could be 2B or we should do something else to print that number as hex, thank you
Was This Post Helpful? 0
  • +
  • -

#9 sssmartie  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 03-June 08


Dream Kudos: 0

Re: Decimal to Hexadecimal

Posted 03 June 2008 - 02:40 AM

hmmm :blink: don't the components of an arrays always have to be the same type...like I mean can we place both a number and a character in an array?!...if so what type of array should it be defined as in the begining of the program?[font=Comic Sans Ms]
Was This Post Helpful? 0
  • +
  • -

#10 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • Icon

Reputation: 143
  • View blog
  • Posts: 4,636
  • Joined: 26-November 04


Dream Kudos: 2825

Expert In: J2ME, 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

Re: Decimal to Hexadecimal

Posted 04 June 2008 - 01:09 AM

A character is stored in memory as a number as per its ASCII code.
So although integer and character sound like two completely different data types, they are actually the same except that an integer takes 2/4 bytes on 16/32-bit systems while char takes up only 1 byte.
Was This Post Helpful? 1
  • +
  • -

#11 sssmartie  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 03-June 08


Dream Kudos: 0

Re: Decimal to Hexadecimal

Posted 05 June 2008 - 07:06 AM

So as your algorithm when we want to print out the hexadecimal number ,if one of the digits would be 10 for example,is it going to be shown as 65 (the ascii code of 'A') or as A itself?
Thank you
Was This Post Helpful? 0
  • +
  • -

#12 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • Icon

Reputation: 143
  • View blog
  • Posts: 4,636
  • Joined: 26-November 04


Dream Kudos: 2825

Expert In: J2ME, 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

Re: Decimal to Hexadecimal

Posted 06 June 2008 - 12:08 AM

It will be stored as 65 in the Array.
However, since the data type of the array is char, cout will interpret it as a set of characters and print it as a character i.e. 'A'

The same applies to printf().

Try this out to have a better idea.
char c = 'A';
int i = 66;

printf("\n%d",c);
printf("\n%c",i);


:)
Was This Post Helpful? 1
  • +
  • -

#13 h3X  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 30-January 09


Dream Kudos: 0

Re: Decimal to Hexadecimal

Posted 30 January 2009 - 08:20 AM

I wrote this in C++ before I saw this thread, and i just thought I'd share it with you:

#include <cstdio>
#include <cstdlib>
#include <stack>
#include <string>

using std::string;
using std::stack;

string dec2hex(int dec)
{
	int i = 0;
	stack <int>remainder;
	string hex, temp;
	char hexDigits[] = { "0123456789ABCDEF" };

	while (dec != 0)
	{
		remainder.push(dec % 16);
		dec /= 16;
		++i;
	}
	
	while (i != 0)
	{
		if (remainder.top() > 15)
		{
			temp = dec2hex(remainder.top());
			hex += temp;
		}
		hex.push_back(hexDigits[remainder.top()]);
		remainder.pop();
		--i;
	}
	return hex;
}

int main(int argc, char *argv[])
{
	if (argc != 2)
		exit(printf("Error: expected 1 param\nUsage: %s <integer>\n", argv[0]));

	int dec = atoi(argv[1]);
	
	string hex = dec2hex(dec);
	printf("%s\n", hex.c_str());
	
	return 0;
}



I hope someone finds this useful. :)
Was This Post Helpful? 0
  • +
  • -

#14 Guest_Ankit Tripathi*


Reputation:

Re: Decimal to Hexadecimal

Posted 22 March 2010 - 01:24 AM

#include "dec2hex.h"


DEC2HEX :: DEC2HEX (const int DEC_NUM)
{
    _number_ = DEC_NUM;
    _ERROR_ = 0;
    printf("DEC2HEX:: Converting %d to Hexadecimal\n", _number_);
    memset(_HEXA_, '\0', 50);
    if(ConvertToHex())
    {
        _ERROR_ = 1;
    }
}

DEC2HEX :: ~DEC2HEX ()
{
    if(_ERROR_)
    {
        printf("DECIMAL TO HEXADECIMAL FAILED\n");
    }
    else
    {
        printf("THE HEXADECIMAL NUMBER IS :: %s\n", _HEXA_);
    }
}

int DEC2HEX :: ConvertToHex ()
{
    int  _remainder_ = 0, _quotient_ = 0, temp_number = 0, _cntr_ = 0;
    char _temp_HEXA_[50]; char _remain_ = '\0';
    memset(_temp_HEXA_, '\0', 50);

    temp_number = _number_;

    while (TRUE)
    {
       _quotient_ = temp_number / 16;
       _remainder_ = temp_number % 16;

       _remain_ = '\0';
       switch(_remainder_)
       {
           case 0  : _remain_ = '0'; break;
           case 1  : _remain_ = '1'; break;
           case 2  : _remain_ = '2'; break;
           case 3  : _remain_ = '3'; break;
           case 4  : _remain_ = '4'; break;
           case 5  : _remain_ = '5'; break;
           case 6  : _remain_ = '6'; break;
           case 7  : _remain_ = '7'; break;
           case 8  : _remain_ = '8'; break;
           case 9  : _remain_ = '9'; break;
           case 10 : _remain_ = 'A'; break;
           case 11 : _remain_ = 'B'; break;
           case 12 : _remain_ = 'C'; break;
           case 13 : _remain_ = 'D'; break;
           case 14 : _remain_ = 'E'; break;
           case 15 : _remain_ = 'F'; break;
       }

       temp_number = _quotient_;
       _temp_HEXA_[_cntr_] = _remain_;
       _cntr_++;
       if(!temp_number)
       {
           break;
       }
    }
    GenerateAnswer(_temp_HEXA_); /** Answer in _temp_HEXA_ is in reverse order **/
    return 0;
}

int DEC2HEX :: GenerateAnswer (const char *RevAnswer)
{
    char CorrectAnswer[50]; memset(CorrectAnswer, '\0', 50);
    int  AnswerLen = 0, _cntr_ = 0, _rev_cntr_ = 0;
    AnswerLen = strlen(RevAnswer);
    _rev_cntr_ = AnswerLen - 1;
    while(TRUE)
    {
        AnswerLen--;
        CorrectAnswer[_cntr_] = *(RevAnswer + AnswerLen);
        _cntr_++;
        if(!AnswerLen)
        {
            break;
        }
    }
    strcpy(_HEXA_, CorrectAnswer);
    return 0;
}

Was This Post Helpful? 0

#15 Guest_Amarsanaa Mongolia*


Reputation:

Re: Decimal to Hexadecimal

Posted 30 May 2010 - 10:24 PM

hmmm, too many code. Please see this code.

 string dec2hex(int dec)
{
	char hexDigits[] = {"0123456789ABCDEF"};
	unsigned char a = 0;
	string hex;
	while(dec != 0) {
		a = dec & 0xF;
		hex.insert(hex.begin(), hexDigits[a]);
		dec >>= 4;
	}
	return hex;
}




View Posth3X, on 30 January 2009 - 08:20 AM, said:

I wrote this in C++ before I saw this thread, and i just thought I'd share it with you:

#include <cstdio>
#include <cstdlib>
#include <stack>
#include <string>

using std::string;
using std::stack;

string dec2hex(int dec)
{
	int i = 0;
	stack <int>remainder;
	string hex, temp;
	char hexDigits[] = { "0123456789ABCDEF" };

	while (dec != 0)
	{
		remainder.push(dec % 16);
		dec /= 16;
		++i;
	}
	
	while (i != 0)
	{
		if (remainder.top() > 15)
		{
			temp = dec2hex(remainder.top());
			hex += temp;
		}
		hex.push_back(hexDigits[remainder.top()]);
		remainder.pop();
		--i;
	}
	return hex;
}

int main(int argc, char *argv[])
{
	if (argc != 2)
		exit(printf("Error: expected 1 param\nUsage: %s <integer>\n", argv[0]));

	int dec = atoi(argv[1]);
	
	string hex = dec2hex(dec);
	printf("%s\n", hex.c_str());
	
	return 0;
}



I hope someone finds this useful. :)

Was This Post Helpful? 0

  • (2 Pages)
  • +
  • 1
  • 2

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users