Convert int number to corresponding floating value

I read a double word from an omron plc. The value returned is, for example, 1036831949 and it is the floating value 0.1
I've to convert the int value in the decimal value using c# but I don't know how.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

anarki_jimbelSenior DeveloperCommented:
Could you pls explain in more details? E.g., how do you know that integer 1036831949 represents floating value 0.1?
And probably not really 0.1 but 0.1036831949? What rounding precision is required?
For conversion, may be you need simply divide your integer by 10000000000?

Of course, you need to declare your variable as float:

int myInt = 1036831949;
            long coefficient = 10000000000;
            float myFloat = myInt / coefficient;

Open in new window

BTW, keep in mind, that floating and decimal numbers are different types.
How did you read it?
It looks like you should have read it as a float rather than as an int?
Can you convert it with

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
There should be a way for you to read that number as floating point. Converting it by hand is going to be something of a pain. But now that I've gone through the exercise, I'll give you the general idea of what you have to do:

My first step was to see what 1036831949 looked like as hexadecimal: 0x3DCCCCCD - the repeated C hexdigit was encouraging if the value was indeed supposed to be something like 0.10000.

Then I asked Google what a Omron PLC might be and that led to me an indication that the device was likely using standard IEEE754 floating-point representation. For 32-bit words, there is 1 bit for sign, 8 bits for exponent, and 23 bits of mantissa (with a phantom leading 1-bit assumed).

Breaking this down:

The sign bit is 0 (meaning the number is positive).

The exponent bits are 0x7B or 123 decimal. You subtract an offset of 127 from the exponent to go from a range of 1-254 to a range of -126 to 127, so this exponent is -4. This corresponds to dividing by 16.

The mantissa bits are 8CCCCD in hex, but putting the phantom 1 bit back in gives CCCCCD, which is 13,421,773 decimal. To convert that decimal mantissa into a decimal fraction, you have to divide by 2^23 or 8,388,608.

My calculator says this is 1.60000238 (you can see the floating-point error starting to creep in there).

So if you divide the mantissa part by the 16 from the exponent part, you get a value that is indeed very close to the 0.1 value you originally claimed it was.

You can see these calculations worked out with diagrams on this page of an Omron manual:

So, you either have to figure out how to recast this number as a float type in C# or engage in bit slicing and shuffling to imitate the calculations I just did.
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Ozo's got it right (as usual), if you can't read the data in as float in the first place, you can use BitConverter class methods.

First convert to bytes with BitConverter.GetBytes Method (Int32).

Then convert to single-precision float with BitConverter.ToSingle Method.

You can pull up C# example code on both of those pages which, combined, should do the trick for you.
fantamenAuthor Commented:
Thank's a lot.
I've read something about BitConverter but your explanation is very good.
fantamenAuthor Commented:
Hi Ozo and Jmcg

Obviously the inverse operation, from single to int32, is made by:
- convert single value to byte array.
- convert byte array to int32.

Yes, that sequence should work.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.