Hex to Dec in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gingit Dorot
    New Member
    • Jul 2011
    • 1

    Hex to Dec in C

    I am querying a device through an I2C buf for temp, I receive a hex value and I need to convert it to a decimal number to pass on to another device.
    I need to convert for instance 0x21 to 33. I am using a char for both.
  • David H
    New Member
    • Jun 2011
    • 4

    #2
    Look up strtol
    Code:
    long int dec = strtol("0x21", NULL, 16);

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      This may be one way:
      Code:
      int ch,hexnum;
      cout<<"Converts a hex (base 16)format to a decimal format\n\n";
      cout<<"How many hex characters ";
      cin>>ch;
      cout<<"Enter the number (w/- spaces) ";
       
      for(int i=0;i<ch;i++)
      {
      cin>>hexnum; 
      a[i]=hexnum;          
      }
      int x=0;
      for(int i= 0;i<ch;i++)
      {
      x=16*x + a[i];
      if (i>ch)break;}
      cout<<"The dec value of hex ";
      for(int i=0;i<ch;i++)
      if(a[i]==15)cout<<'F';
      else if(a[i]==14)cout<<'E';
      else if(a[i]==13)cout<<'D';
      else if(a[i]==12)cout<<'C';
      else if(a[i]==11)cout<<'B';
      else if(a[i]==10)cout<<'A';
      else cout<<a[i];
      cout<<" is "<<x;
      cout<<endl;

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        You seem to have done it the long way round whodgson for C++ try this

        Code:
        #include <iostream>
        #include <sstream>
        #include <string>
        #include <iomanip>
        #include <exception>
        
        using namespace std;
        
        int main()
        {
        	string input = "5b"; // 91
        
        	istringstream iss(input);
        	
        	int output;
        	
        	iss >> hex >> output;
        	
        	cout << input << " <=> " << output << endl;
        	
        	return 0;
        }
        not to mention that the request was for C and this is clearly C++ :D

        I personally would use David H solution.

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          ...yes Banfa ...agree entirely with you... one of my more pathetic solutions especially as it was not even C. The David H method is brief and simple but haven`t seen that NULL form before. However I assume alas it is also C++. Will study your stream method more closely in due course. Thanks.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            The OP needs to state the problem more precisely.

            When you say "I receive a hex value", do you mean:
            > you receive a binary value;
            > you receive a hex-ASCII value;
            > something else?

            When you say "I need to convert it to a decimal number", do you mean:
            > you need to create a decimal-ASCII value;
            > you need to create a binary value;
            > something else?

            It would help to know how large a number is represented by the "hex value" (1 byte, 2 bytes, etc). If it is more than one byte then that begs the question of endianness.

            Note: when I refer to "hex-ASCII" or "decimal-ASCII", I mean a sequence of characters that encode the value in either hexadecimal or ascii digits. For example, the 1-byte value 0x1A can be encoded as either "1A" or "26". Both of those encodings are 2 bytes long.

            Comment

            Working...