## Converting Big Endian to decimal

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Steve001
Valued Contributor
Posts: 1107
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 454 times
Been thanked: 509 times
Contact:

### Converting Big Endian to decimal

Morning All,

Bit stuck again can somebody explain how to convert Float – Big Endian (ABCD) to Decimal please ?

I would like to understand how

43 6D 05 1F = 237.02

And

42 48 0F 5C = 50.015

i found something on the visual studio pages saying to read the bytes one at a time and reverse them.
I cannot get this to add up ?

Steve
Success always occurs in private and failure in full view.

mnf
Valued Contributor
Posts: 633
Joined: Wed May 31, 2017 11:57 am
Has thanked: 57 times
Been thanked: 350 times
Contact:

### Re: Converting Big Endian to decimal

This works (on an Arduino at least)

float.JPG (23.08 KiB) Viewed 3180 times

Uses an array of 4 bytes 'n and a float f and a string t (meaningful names!?)
The C code puts the hex given into a float in reverse order (so f[3] = h[0]) and converts from hex to a float using a union...
Just output converted value to UART as a test... (In this case 50.014999)

(An alternative approach would be to take the address of the float as a pointer to byte and add the data using that?) - and the second C block shows one way to do this (with the values hardcoded here!) which just pushes the value directly into a FC variable - I would suggest creating a function taking an array of 4 hex digits and returning a float!

Note: You could use a loop with *p++ = FCV_N[3-i] or as in first block p[3-i] = FCV_N[i]) as a more readable alternative in the second C block.

Martin
These users thanked the author mnf for the post:
medelec35 (Mon Aug 26, 2019 10:58 am)
Rating: 5%

mnf
Valued Contributor
Posts: 633
Joined: Wed May 31, 2017 11:57 am
Has thanked: 57 times
Been thanked: 350 times
Contact:

### Re: Converting Big Endian to decimal

More usefully as a macro....
float.fcfx

Martin
These users thanked the author mnf for the post (total 2):
Steve001 (Sun Aug 25, 2019 8:19 am) • medelec35 (Mon Aug 26, 2019 10:58 am)
Rating: 10%

Steve001
Valued Contributor
Posts: 1107
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 454 times
Been thanked: 509 times
Contact:

### Re: Converting Big Endian to decimal

hi Martin

Thank you for that i will have a look later

Steve
Success always occurs in private and failure in full view.

Steve001
Valued Contributor
Posts: 1107
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 454 times
Been thanked: 509 times
Contact:

### Re: Converting Big Endian to decimal

Hi Martin,

sorry i didn't understand it, could you explain how it works and what is happening please
I dont have an adrunio either to try and use.

Steve
Success always occurs in private and failure in full view.

mnf
Valued Contributor
Posts: 633
Joined: Wed May 31, 2017 11:57 am
Has thanked: 57 times
Been thanked: 350 times
Contact:

### Re: Converting Big Endian to decimal

...

The computer memory is an 'array' of 8 bit 'cells' with an address for each (so for an Arduino - the address could be 0..2047 for it 2048 byte RAM)

A 'variable' in a program is actually the address of one or more bytes in memory (so for a float (32 bit) it would point to 4 bytes somewhere in the memory)

So - a float f (32 bit) is stored in memory at 4 consecutive locations which the program can read and write to to manipulate the value of 'f'
'f' might be '12' (the address - not the actual value of the variable) and would reference the memory at addresses 12,13,14,15) - when a program uses a variable - the MCU loads from this memory address into a register - performs some calculation on it - then stores it back...)

The '&' operator gets the address of a variable - and then we 'poke' (to use a basic term) or store the values we want into the memory cells - and we can just treat this address as a pointer to an array (we've specified a point to a byte (MX_UINT8) - so we are just writing a byte at a time - then it's just a matter of storing the values we want in the right order)

Then when our program accesses the variable it retrieves the values we stored - and treats them as a (in this case) floating point value..

The C code won't simulate unfortunately.

More details if you wish?

Martin
These users thanked the author mnf for the post (total 3):
medelec35 (Mon Aug 26, 2019 10:57 am) • Steve001 (Mon Aug 26, 2019 2:33 pm) • Benj (Wed Aug 28, 2019 4:08 pm)
Rating: 15%

Steve001
Valued Contributor
Posts: 1107
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 454 times
Been thanked: 509 times
Contact:

### Re: Converting Big Endian to decimal

Hi Martin,

What I don't under stand is how do I get from :

my 4 bytes of data : 0x43 0x6D 0x05 0x1F

So looking at data stream - this should be 237.02

hex to decimal
43 = 67
6D = 109
05 = 5
1F = 31

so if I simply add those together as first thought = 212

another example I found was to convert the number to binary then apply 2's complement - this turned out to be a load of rubbish.

= 11010100

2's complement

= 00101100

converted to decimal = 44

Steve
Success always occurs in private and failure in full view.

mnf
Valued Contributor
Posts: 633
Joined: Wed May 31, 2017 11:57 am
Has thanked: 57 times
Been thanked: 350 times
Contact:

### Re: Converting Big Endian to decimal

So the 4 bytes represent a 32 bit (4x8) floating point number.... Adding them doesn't do what you want - they are just a representation of the underlying 32 bits of data...

See https://en.wikipedia.org/wiki/IEEE_754 if you want a summary of the full low down (and I suspect IEEE754 is a huge document - for the ultimate go to guide) as to how the binary represents the float.

Martin
These users thanked the author mnf for the post:
Steve001 (Mon Aug 26, 2019 7:03 pm)
Rating: 5%

Steve001
Valued Contributor
Posts: 1107
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 454 times
Been thanked: 509 times
Contact:

### Re: Converting Big Endian to decimal

Hi Martin

thank you wow ... that's a pretty hefty document

http://www.thefullwiki.org/IEEE_754-2008

after a quick look I am going about it incorrectly,

Steve
Success always occurs in private and failure in full view.

mnf
Valued Contributor
Posts: 633
Joined: Wed May 31, 2017 11:57 am
Has thanked: 57 times
Been thanked: 350 times
Contact:

### Re: Converting Big Endian to decimal

Tambala
Posts: 1
Joined: Mon Sep 09, 2019 1:08 pm
Contact:

### Re: Converting Big Endian to decimal

mnf wrote:I used https://www.scadacore.com/tools/programming-calculators/online-hex-converter/ to check the required layout.

Martin

First time I see this website! Thank you very much for sharing this information, will try to use it soon for my work.