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 can I get each byte of 32-bit floating point by using C

Status
Not open for further replies.

BabyTiger

Full Member level 2
Joined
Aug 8, 2005
Messages
123
Helped
4
Reputation
8
Reaction score
2
Trophy points
1,298
Activity points
2,126
I need to get each byte of 32-bit floating point variable by using C programming
Ex. Variable Z is the floating point has 4 byts (BYTE0:BYTE1:BYTE2:BYTE3)
I need to put
BYTE0 into A where A is unsigned char
BYTE1 into B where B is unsigned char
BYTE2 into C where C is unsigned char
BYTE3 into D where D is unsigned char

Please help me, I need to write in C programming.

Best Regards
 

Re: How can I get each byte of 32-bit floating point by usin

Here you go.

Code:
#include <stdio.h>
#include <conio.h>


void main(void)
{
	float x;
	unsigned char bytes[4];

	x = 1087.9845F;

	*(float *)(&bytes[0]) = *(float *)(&x);

	printf("B0=%x\nB1=%x\nB2=%x\nB3=%x\n",bytes[0],bytes[1],bytes[2],bytes[3]);
}
 

Re: How can I get each byte of 32-bit floating point by usin

Using UNION

main()
{
union
{ unsigned char ch[4];
float d;
}s;

s.d=0xffffffffh;

}

you do as above
the BYTE0 of variable d will be into the ch[0]
the BYTE1 of variable d will be into the ch[1]
the BYTE2 of variable d will be into the ch[2]
the BYTE3 of variable d will be into the ch[3]
 

Re: How can I get each byte of 32-bit floating point by usin

This will work:

//**********************************
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{

float f = 3332.54543;
unsigned long *l;
unsigned char *p;

l = (unsigned long *)&f;
p = (unsigned char *)&f;

printf("Here they are: %lx, %hx, %hx, %hx, %hx\n", *l, *p, *(p+1), *(p+2), *(p+3));

system("PAUSE");
return 0;
}

//*************************************

This is the output:

Here they are: 455048ba, ba, 48, 50, 45


I used the devc compiler.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top