|
-
Mar 31st, 2003, 01:32 AM
#1
Thread Starter
Addicted Member
hex to decimal conversion !
hello !
how can i convert a hex to decimal in VC++?
any function available
-
Mar 31st, 2003, 04:06 AM
#2
transcendental analytic
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Mar 31st, 2003, 05:56 AM
#3
Member
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
-
Mar 31st, 2003, 12:26 PM
#4
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:
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();
}
should work.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 31st, 2003, 05:52 PM
#5
Frenzied Member
Hi CB,
The code above tries to modify something that's const.
Code:
void hextodecimal(const string &in, const string &out)
so that should be
Code:
void hextodecimal(const string &in, string &out)
or if you want the function to return a value (which I think is more logical in this case)
Code:
string hextodecimal(const string &in)
{
int temp;
istringstream iss(in);
iss >> hex >> temp;
ostringstream oss;
oss << temp;
return oss.str();
}
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Mar 31st, 2003, 11:28 PM
#6
Thread Starter
Addicted Member
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 !!!
-
Apr 1st, 2003, 03:50 AM
#7
transcendental analytic
atoi what was i thinking its strtol as CB said, here's an example of how to use it:
http://www.cplusplus.com/ref/cstdlib/strtol.html
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Apr 1st, 2003, 04:17 AM
#8
Hyperactive Member
Here's another alternate way.
Code:
#include<stdio.h>
void HexToDec(const char *HexStr, int *DecNum)
{
sscanf(HexStr,"%x",DecNum);
return;
}
You can take it out of the function if you want.
-
Apr 15th, 2003, 01:42 AM
#9
Originally posted by Jop
Hi CB,
The code above tries to modify something that's const.
Code:
void hextodecimal(const string &in, const string &out)
so that should be
Code:
void hextodecimal(const string &in, string &out)
or if you want the function to return a value (which I think is more logical in this case)
Code:
string hextodecimal(const string &in)
{
int temp;
istringstream iss(in);
iss >> hex >> temp;
ostringstream oss;
oss << temp;
return oss.str();
}
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|