convert a hex variable of type "0xffffffff" to a decimal variable "4294967295"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bravos
    New Member
    • Aug 2018
    • 2

    convert a hex variable of type "0xffffffff" to a decimal variable "4294967295"

    Hello everyone.
    I'm new to c, and I'm not sure how to convert a hex variable of type "0xffffffff " to a decimal variable "4294967295 ". I look for examples or an explained way of doing.
    Thanks in advance for your help.
    Bravos
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Integer variables within your program are neither "hex" or "decimal" -- they are binary.

    1. How does the hex value get into your program?

    2. How does the decimal value get out of your program?

    If the user types in the hex value when prompted by your program then what get-input function are you planning to use?

    If the program prints the decimal value on the console for the user to read then what put-output function are you planning to use?

    Comment

    • bravos
      New Member
      • Aug 2018
      • 2

      #3
      Donbock, thanks for the quick response.
      Talking about the problem a little. The variable contains in hex the value of a sensor, which is read by i2c, which has 3byts which are grouped into a variable with the statement for example
      "test = ((data_cap [0] << 16) | data_cap [1] << 8 ) | data_cap [2]);
      So far so good.
      How does the hex value get into your program?
      I need to show the result on an LCD, even without being normalized (either with "media" or "kalman filter").
      I think there is a simple way to do it, but I do not know how.
      After being converted to a decimal it is simple to display on an LCD.
      Thank you.
      Bravos
      Last edited by zmbd; Aug 22 '18, 08:37 PM. Reason: [z{placed inline-code format and quote format}]

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Let's look at the three bytes you obtain via i2c from the sensor.
        The way you are shifting and or-ing those bytes together makes it look like the sensor provides a single 24-bit value. If so, then there is no hex value -- only a 24-bit binary value.

        How do you cause a value to be displayed on the LCD?
        (How many LCD digits are there?)

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          I'm confused. There is no hex type in C.

          So your "0xffffffff " is a string. That is, a char array.

          So A2F is a string.

          So the string element 0 is A. The position in the string is the value of A. In this case A is the 3rd digitso the valus of that column (in hex) is 256.


          So multiply 256 by the value of A (which is 10 in decimal) to get 2560.


          Repeat using the next char in the string. In this case the 2. In hex this position has a value of 16. So multiply 2 by 16 to get 32 and add it to 2560 to get 2592.


          Finally the F has a value of 1. So multiply 15 by 1 to get 15. Add this to 2592 to get 2607. The integer 2607 is A2F in hex.


          Usually all there is in the integer is a binary number which you can display as binary, decimal or hex.

          Have I helped or just made this worse?
          Last edited by weaknessforcats; Aug 23 '18, 09:41 PM.

          Comment

          Working...