|
|
View previous topic :: View next topic |
Author |
Message |
Kimi
Joined: 30 Jan 2005 Posts: 23 Location: Argentina
|
Data Type Conversion |
Posted: Fri Feb 18, 2005 10:14 am |
|
|
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
|
Re: Data Type Conversion |
Posted: Fri Feb 18, 2005 10:23 am |
|
|
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 |
Posted: Fri Feb 18, 2005 3:49 pm |
|
|
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
|
|
Posted: Sat Feb 19, 2005 5:34 am |
|
|
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! |
|
|
|
|
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
|