Convert Hex to Dec! Help acquired!

Hello everyone!

I have some problems with my MAX31850 Sensor. I want to readout his address. One time as Hex and the other time as Dec. I’m using the MAX31850 DallasTemperature and MAX31850 OneWire Library.
My Hex-number readout is as followed:

void printAddressHEX(DeviceAddress deviceAddress){
for (uint8_t i = 0; i < 8; i++){
if (deviceAddress < 16) Serial.print(“0”);{
_ Serial.print(deviceAddress*, HEX);_
_
}_
_
}_
_
}_
I’m not even sure if there’s the mistake. But further it goes: My Dec-number readout part:
uint32_t printAddressDEC(DeviceAddress deviceAddress){
uint32_t a0;
uint32_t a1;
uint64_t address;
_
float i;*_

* for (uint64_t i = 0; i < 4; i++){
a0 += (uint32_t)deviceAddress_<<(3-i)8;
a1 += ((uint32_t)deviceAddress[i+4])<<(3-i)8;
address += deviceAddress _ pow(256,(7-i));

return address;
_}

I solved the conversion with bit-pushing (if it’s the right term). I tried to put the whole Hex-number into two variables because of the issue that you can’t print a 64-Bit unit.
Two problems now:
First: I want to use the variable “address” out of that function to tell me in one step what address in Dec we have. But even in there it tells me that address has nothing in it…
Second: The Hex- and the Dec-number are not the same. But when I try to use my Hex to Dec translation (printAddressDEC) with a given variable as Hex-number it works. I really don’t know wheres the problem in here…
Thank you in advance for your answer! I hope you can help me! If more information is needed do not worry to ask!_

Please correct your post above and add code tags around your code: [code][color=blue]// your code is here[/color][/code].

It should look like this:// your code is here (Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)


just curious - what's the use case for printing these 8 bytes from DeviceAddress as a long long in DEC ? I can't see any value for doing so...

The device address is binary.

This incorrectly printed line (because you left out the code tags) prints out binary values in hexadecimal.

Serial.print(deviceAddress, HEX);

To print values in decimal, leave out the ", HEX".

azula: I'm not even sure if there's the mistake. I solved the conversion with bit-pushing

Go to File -> Preferences and make sure Compiler Warnings are set to ALL.

  • I am certain the compiler will give you a warning that you set a0 and a1, but you did not use it. This makes it unlikely that your code is working.

  • The function is called print but does not print anything.

  • The function returns a 32-bit number made from a 64-bit number. That does not work without loosing data.

As J-M-L said printing the address as decimal does not make a lot of sense. It is going to be a big number and it will be far more difficult to read because it has 8 Digits as hexadecimal and up to 19 or 20 as decimal. I am not too bad at math but numbers that size I count the digits with my fingers pointing at the display. :)

jremington: The device address is binary.

Kind of. Its perhaps better described just as a “64-bit unsigned integer”.

For me “hex”, “decimal” and “binary” are human readable number formats which are applied to integers when they are converted to ASCII (or some other character encoding) for display.

Until that happens they are just integers, and don’t have an inherent “format”.

Although of course at the machine level all data is a sequence of bits, and a bit is a single binary digit. So I get what you are saying, I’m just attempting to point out that to a beginner saying it is “binary” may not help their understanding of data representation much... sorry!

My goodness....

 address += deviceAddress * pow(256,(7-i));

...had the author never heard of bit shifting?

Kind of. Its perhaps better described just as a “64-bit unsigned integer”.

Just see that as an ID.

it is a sequence of 8 bytes and there is no gain in seing this as a 64-bit unsigned integer…

it’s like an IP address. you write 192.168.25.32, which is 4 bytes 0xC0 0xA8 0x19 and 0x20

you could sure see this as 0xC0A81920 if you are big endian or 0x2019A8C0 if you are little endian and say my IP address is 3232241952(dec) (big endian) or 538552512(dec) (little endian) but really whilst I can easily remember 192.168.25.32, I would have a hard time with the decimal version.

Worse in little endian, the “next logical IP address”, 192.168.25.33, is represented with 4 bytes 0xC0 0xA8 0x19 and 0x21 becomes 0x2119A8C0 which is 555329728(dec) ==> that’s 16777216 (224) units appart…

Anyway - I basically I think there is zero value in trying to print an ID as a decimal number…

Its perhaps better described just as a "64-bit unsigned integer".

Which is binary in the computer. As mentioned above, it is not useful or better to group the bits into a single quantity.