LB Booster
« Simulating a union »

Welcome Guest. Please Login or Register.
Apr 1st, 2018, 03:46am



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
We apologize Conforums does not have any export functions to migrate data.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

Thank you Conforums members.
Speed up Liberty BASIC programs by up to ten times!
Compile Liberty BASIC programs to compact, standalone executables!
Overcome many of Liberty BASIC's bugs and limitations!
LB Booster Resources
LB Booster documentation
LB Booster Home Page
LB Booster technical Wiki
Just BASIC forum
BBC BASIC Home Page
Liberty BASIC forum (the original)

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Simulating a union  (Read 210 times)
Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Simulating a union
« Thread started on: Sep 9th, 2016, 2:05pm »

I received this query by email: "How can I convert an 8-byte/64bit string containing a IEEE754 double precision number into the numeric value itself". This is a problem of converting a data structure from one representation to another, something which in other languages would often be achieved using a union.

Liberty BASIC doesn't have unions, and nor does LBB, but at least in LBB it is easy to simulate one. This is the solution I offered the enquirer:

Code:
    struct union1, string as char[9]
    struct union2, number as double, pad as char[1]
    union2.struct = union1.struct
    
    union1.string.struct = chr$(119)+chr$(190)+chr$(159)+chr$(26)+ _
                           chr$(47)+chr$(221)+chr$(94)+chr$(64)
                           
    print union2.number.struct 

Here I create two structures, union1 and union2 (which are the same size) and then I set their addresses to be equal. Now anything written into one of the structures can be read from the other in an alternative format; the conversion can just as easily be done in the other direction. I've written up this technique at the LBB Wiki.

This method doesn't work in LB 4, and normally I'm not keen to promote LB-specific workarounds here, but for the record this is how the same conversion can be achieved if for some reason you're not willing or able to use LBB:

Code:
    struct union1, string as char[9]
    struct union2, number as double, pad as char[1]
    struct temp, temp as ptr

    union1.string.struct = chr$(119)+chr$(190)+chr$(159)+chr$(26)+ _
                           chr$(47)+chr$(221)+chr$(94)+chr$(64)

    temp.temp.struct = union1.struct
    union2.struct = temp.temp.struct

    print union2.number.struct 

Richard.
« Last Edit: Sep 9th, 2016, 8:59pm by Richard Russell » User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls