Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to extract the low byte of a float variable and store it in a char variable?

Status
Not open for further replies.

mana111

Member level 1
Joined
Nov 22, 2005
Messages
32
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,569
hi
i need to extract the low byte of a float variable and store it in a char variable

any suggestions
thanx
 

Re: qustion for C expert

It depends byte ordering in the machine. i.e. the machine is little endian or big endian.
Here is a C code for little endian

float a; // float variable whose low byte is needed
char *ptr, b;

ptr = (char *)&a;

b = ptr[0]; // b contains the low byte of a now.

.. Hope this is what u wanted.
 

    mana111

    Points: 2
    Helpful Answer Positive Rating
qustion for C expert

#define LOBYTE(a) (*((unsigned char*)&(a)))
#define HIBYTE(a) (*((unsigned char*)&(a) + 1))
 

Re: qustion for C expert

booklog said:
It depends byte ordering in the machine. i.e. the machine is little endian or big endian.
Here is a C code for little endian

float a; // float variable whose low byte is needed
char *ptr, b;

ptr = (char *)&a;

b = ptr[0]; // b contains the low byte of a now.

.. Hope this is what u wanted.

Hi,

Why did you say b = ptr[0]; instead of b = *ptr;

Thanks alot
 

Re: qustion for C expert

Here's a way to do it that's independent of endianness:

Code:
char GetLsb( float *f ) 
{ 
    int a=1;
    return ((char *)f)[ (sizeof(float)-1)*!(*(char *)&a) ];
}

float f = 123.456;
char  c = GetLsb(&f);

The above was modified per booklog's suggestion (see next post) because I got too clever and tried to take the address of a constant.
 

Re: qustion for C expert

Hi Lambtron,
Whatever u have posted, need some modification.
The correct code is as below
char GetLsb( float *f )
{
int a = 5 ;
return ((char *)f)[ (((char*)&a)[0] == 5)) ? 0 : (sizeof(float)-1) ];
}
 

qustion for C expert

sorry i tried it but too many errors

please i want to convert a log routine from basic to c and i know that there is a log function but i need it detailed as it in the basic code

can anyone convert this for me please
---------------------------------------------------------------
' Create some variables for use with LN and LOG

Dim LOG_VALUE as Float
Dim LOG_POWER as Float
Dim LOG_TEMP as Float
Dim LOG_X as Float
Dim LOG_FACTOR as Float
Dim LOG_XSQR as Float
Dim LOG_N as Byte
Dim LOG_TEMP2 as Byte



LNc:

' We can't have ln(1) so we must return a zero if it is
If LOG_VALUE.Byte0 = 0 Then LOG_VALUE = 0: Return

' The difference between LOG_N.BYTE0 and $7E will be the
' amount of 2^LOG_N's that we want to multiply times ln(2)
If LOG_VALUE.Byte0 <= $7E Then
LOG_N = $7E - LOG_VALUE.Byte0
LOG_FACTOR = -0.69314718 * LOG_N
Else
LOG_N = LOG_VALUE.Byte0 - $7E
LOG_FACTOR = 0.69314718 * LOG_N
Endif
LOG_VALUE.Byte0 = $7E

' Begin the taylor series expansion
' ln(1+LOG_X) = LOG_X - (LOG_X^2/2) + (LOG_X^3/3) -+...

LOG_VALUE = LOG_VALUE - 1
LOG_X = LOG_VALUE
LOG_XSQR = LOG_VALUE
LOG_N = 2
Repeat
LOG_XSQR = LOG_XSQR * LOG_X
LOG_VALUE = LOG_VALUE - (LOG_XSQR / LOG_N)
LOG_XSQR = LOG_XSQR * LOG_X
LOG_TEMP2 = (LOG_N + 1)
LOG_VALUE = LOG_VALUE + (LOG_XSQR / LOG_TEMP2)
LOG_N = LOG_N + 2
Until LOG_N > 12
LOG_VALUE = LOG_VALUE + LOG_FACTOR
Return
-----------------------------------------------------------
thanx alot
 

Re: qustion for C expert

I think structure and union is best

union
{
float value ;

struct
{
unsigned char value_a0 ;
unsigned char value_a1 ;
unsigned char value_a2 ;
unsigned char value_a3 ;
}
split
}
floatcombo ;

floatcombo.value = 323947.24742424 ;

floatcombo.split.value_aX is now contais 4 bytes of float variable
 

qustion for C expert

atmelAVR91
i dont know how to use it in my code
could you please write my basic code using your method?
thankx
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top