Results 1 to 9 of 9

Thread: hex to decimal conversion !

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    166

    hex to decimal conversion !

    hello !

    how can i convert a hex to decimal in VC++?
    any function available

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    use atoi
    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.

  3. #3
    Member
    Join Date
    Sep 2001
    Posts
    42
    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

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  5. #5
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    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.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    166
    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 !!!

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  8. #8
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    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.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width