hello !
how can i convert a hex to decimal in VC++?
any function available
Printable View
hello !
how can i convert a hex to decimal in VC++?
any function available
use atoi
Hi!!
There is no direct function(as far as i know) to convert a hex number to decimal, but it is very easy to write to simple function for that.
Regards
Shaunak
Conversion from hex to decimal... do you mean from hexadecimal representation to decimal representation or from hexadecimal representation to a real number?
Case b: use strtoi
Case a:
should work.Code:#include <string>
#include <sstream>
#include <iomanip>
using std::string;
using std::ostringstream;
using std::istringstream;
using std::hex;
void hextodecimal(const string &in, const string &out)
{
int temp;
istringstream iss(in);
iss >> hex >> temp;
ostringstream oss;
oss << temp;
out = oss.str();
}
Hi CB,
The code above tries to modify something that's const.
so that should beCode:void hextodecimal(const string &in, const string &out)
or if you want the function to return a value (which I think is more logical in this case)Code:void hextodecimal(const string &in, string &out)
Code:string hextodecimal(const string &in)
{
int temp;
istringstream iss(in);
iss >> hex >> temp;
ostringstream oss;
oss << temp;
return oss.str();
}
thanks !
can we write hex to decimal conversion code in C ???
pls tell me... its sor of very urgent, my entire project is held up due to this
awaiting for a prompt reply
shruti !!!
atoi what was i thinking :o its strtol as CB said, here's an example of how to use it:
http://www.cplusplus.com/ref/cstdlib/strtol.html
Here's another alternate way.
You can take it out of the function if you want.Code:#include<stdio.h>
void HexToDec(const char *HexStr, int *DecNum)
{
sscanf(HexStr,"%x",DecNum);
return;
}
Oops, right. But I don't want to return a string object, that involves too much memory copying IMO. So the non-const reference was what I wanted.Quote:
Originally posted by Jop
Hi CB,
The code above tries to modify something that's const.
so that should beCode:void hextodecimal(const string &in, const string &out)
or if you want the function to return a value (which I think is more logical in this case)Code:void hextodecimal(const string &in, string &out)
Code:string hextodecimal(const string &in)
{
int temp;
istringstream iss(in);
iss >> hex >> temp;
ostringstream oss;
oss << temp;
return oss.str();
}