473,399 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Conversion of float to byte value in C language

Hi,
I am using the Diab compiler for the MPC 555 controller. I wanted to know the internal bit representation of float value. Also if I type cast of float value to byte the way the final value that will be computed.

The code snippet is as follows:

typedef float F32;
typedef unsigned char U8;
typedef unsigned long int U32;

U32 my function(void)
{
U32 cs_sum = 0;
F32 cs_data = 50.157;

cs_sum = (U8) (cs_data);
return cs_data;
}
Feb 9 '07 #1
2 5150
sicarie
4,677 Expert Mod 4TB
Hi,
I am using the Diab compiler for the MPC 555 controller. I wanted to know the internal bit representation of float value. Also if I type cast of float value to byte the way the final value that will be computed.

The code snippet is as follows:

typedef float F32;
typedef unsigned char U8;
typedef unsigned long int U32;

U32 my function(void)
{
U32 cs_sum = 0;
F32 cs_data = 50.157;

cs_sum = (U8) (cs_data);
return cs_data;
}
I'm not sure I understand your code or the second part of your question, but I think - please someone correct me if I'm wrong (which I have been more often than not lately - I'm rusty on my C) - that you can create the float you would like to see, and then use C's printf() command. You can use the first part of the argument to set the value to whichever representation you want, and then give it your float variable after (though you might have to cast before you use printf(), I can't remember).
Feb 10 '07 #2
RRick
463 Expert 256MB
The internal representation of the float or double is system dependent. You might find some info in the system or compiler include files. For linux, /usr/include/limits.h and some math includes under /usr/include/bits have info.

Since the format of floats and ints are radically different, usually you let the compiler do the conversions. Your example would convert a float to a U8 and then put the results into a U32. The end result would be 0...255, and this sounds like you're going to loose info.

Usually float to int conversions drop the fractional part and keep the whole integer. Some systems use rounding. You also have the problem of putting something too large into U8 or U32. Once again it's system dependent what happens.

Try a few tests and see what happens.
Feb 10 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Alexander Eisenhuth | last post by:
Hallo all there, maby I don't see the forest because of all that trees, but : from struct import * # how can I convert f = float(0.5)
8
by: CAFxX | last post by:
i'm writing a program that executes some calculations on a bitmap loaded in memory. these calculation ends up with pixel wth values far over 255, but i need them to be between 0 and 255 since i...
6
by: Arne Schmitz | last post by:
I guess this has been asked before, but I cannot find any answer to this problem. I have program like this: ---SNIP--- #include <cassert> #include <cstdlib> class C { public:
7
by: Mike | last post by:
I am trying to calculate a 32bit float value from 4 int values. I sucessfully calcluated a 32bit long value: LONG l32BitData = pData; l32BitData <<= 8; l32BitData |= (byte)pData;...
4
by: Ken Tough | last post by:
Seems like a simple thing to find out, but I'm struggling. I have googled, but everything I find is about implicit conversion, not explicit. Is this implementation-specific, or does ANSI/ISO...
10
by: pavithra.eswaran | last post by:
Hi, I would like to convert a single precision hexadecimal number to floating point. The following program seems to work fine.. But I do not want to use scanf. I already have a 32 bit hexadecimal...
5
by: Pavils Jurjans | last post by:
Hello, I am somewhat lost in the implicit/expicit possible/impossible type casting in C#... I need to write a class, which among other things, must have wat to read a numeric value type, and...
24
by: Rajesh S R | last post by:
Isn't this code violation of C99 standard? #include <stdio.h> int main( void ) { float a = 0.7; if(a < 0.7) printf("Wrong"); else
1
by: krishna81m | last post by:
I am a newbie and have been trying to understand conversion from double to int and then back to int using the following code was posted on the c++ google group. Could someone help me out with...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.