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!
|
||
Decimal to Hexadecimal
#2
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.
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.
#5
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.
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
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
#8
Re: Decimal to Hexadecimal
Posted 02 April 2008 - 08:41 PM
born2c0de, 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
#9
Re: Decimal to Hexadecimal
Posted 03 June 2008 - 02:40 AM
hmmm
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]
#10
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.
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.
#12
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.
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);
#13
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:
I hope someone finds this useful.
#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.
#14 Guest_Ankit Tripathi*
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;
}
#15 Guest_Amarsanaa Mongolia*
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;
}
h3X, 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:
I hope someone finds this useful.
#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.


Ask A New Question
This topic is locked



MultiQuote








|