424,984 Members | 997 Online
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

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

 P: 2 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 Aug 22 '18 #1
4 Replies

 Expert 100+ P: 2,396 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? Aug 22 '18 #2

 P: 2 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 Aug 22 '18 #3

 Expert 100+ P: 2,396 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?) Aug 22 '18 #4

 Expert Mod 5K+ P: 9,197 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? Aug 22 '18 #5