CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Data Type Conversion

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Kimi



Joined: 30 Jan 2005
Posts: 23
Location: Argentina

View user's profile Send private message Send e-mail Yahoo Messenger MSN Messenger

Data Type Conversion
PostPosted: Fri Feb 18, 2005 10:14 am     Reply with quote

Hello

I need tomake a function that takes a float or a int32, and descompose it in 4 bytes.
And then another one, that takes 4 bytes and make the 32bits variable.

How can I do this?

I tried make32 to make a int32 from 4 bytes, but don't know how to make the reverse process, also, can I use make32 with float variables?

Thanks
Kimi
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

Re: Data Type Conversion
PostPosted: Fri Feb 18, 2005 10:23 am     Reply with quote

Kimi wrote:
Hello

I need tomake a function that takes a float or a int32, and descompose it in 4 bytes.
And then another one, that takes 4 bytes and make the 32bits variable.

How can I do this?

I tried make32 to make a int32 from 4 bytes, but don't know how to make the reverse process, also, can I use make32 with float variables?

Thanks
Kimi


make8()
Ttelmah
Guest







Re: Data Type Conversion
PostPosted: Fri Feb 18, 2005 3:49 pm     Reply with quote

Kimi wrote:
Hello

I need tomake a function that takes a float or a int32, and descompose it in 4 bytes.
And then another one, that takes 4 bytes and make the 32bits variable.

How can I do this?

I tried make32 to make a int32 from 4 bytes, but don't know how to make the reverse process, also, can I use make32 with float variables?

Thanks
Kimi

The 'cleanest' way, (since it will work with other compilers, which the 'makeX' functions won't), is to use a union.
If you declare a variable as:

Code:

union splitter {
    int8 b[4];
    float fval;
    int32 wval;
};
union splitter part_access;

Then 'part_access.wval', is a 32bit variable, which you can write/read when wanted. Similarly, 'part_access.fval', is a float variable occupying the same memory area, and 'part_access.b[0]'... 'part_access.b[3]', are the individual bytes.
The 'nice' thing about this, is that you can put the bytes together, and take them apart as required, and you can even change things 'on the fly'. So (for instance), if you wanted to access the first byte of an existing float variable, you could declare a routine as:
Code:

int8 get_first_byte(union splitter part) {
    return(part.b[0]);
}

This can be called with the float variable, and the compiler will automatically locate the first byte of this variable, and return it. The same could be done with an int32.
Even better, the same 'code', can be generated as a macro, rather than a function.

Best Wishes
Kimi



Joined: 30 Jan 2005
Posts: 23
Location: Argentina

View user's profile Send private message Send e-mail Yahoo Messenger MSN Messenger

PostPosted: Sat Feb 19, 2005 5:34 am     Reply with quote

It's difficult to explain the main reason, as English is not my native language...but one of the most important reasons is that a binary search is faster!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group