C style unions in Xojo?

  1. 2 years ago

    Wes W

    25 Jul 2017 Pre-Release Testers, Xojo Pro MD, Columbia USA

    I am working on a project that communicates serially with a device and I'm finding that I'd like to use something like a C style union to allow me to easily look at the data in different formats. Unions provide a way to freely convert data from one format to another, simply by writing the data into the Union as one data format, and reading the data back as another. Is something like this available in Xojo?

    If a union like mechanism isn't available could the same thing be accomplished by defining several structures that point to the same block of memory?

    I'm pretty new to Xojo so forgive me if I've overlooked something obvious.

    -Wes

  2. Dave S

    25 Jul 2017 San Diego, California USA

    perhaps a memoryblock might help replicate the functions you seek.....(based solely on your post, as I have never used "C" unions)

  3. Jean-Yves P

    25 Jul 2017 Pre-Release Testers, Xojo Pro Europe (France, Besançon)

    yes a memory block could do it
    also a xojo structure as you can get the content of the datas as a bytes array
    http://developer.xojo.com/structure

  4. Norman P

    25 Jul 2017 Xojo Inc, Pre-Release Testers, Xojo Pro Seeking work. npalardy@great-w...

    @Wes W I am working on a project that communicates serially with a device and I'm finding that I'd like to use something like a C style union to allow me to easily look at the data in different formats. Unions provide a way to freely convert data from one format to another, simply by writing the data into the Union as one data format, and reading the data back as another. Is something like this available in Xojo?

    If a union like mechanism isn't available could the same thing be accomplished by defining several structures that point to the same block of memory?

    I'm pretty new to Xojo so forgive me if I've overlooked something obvious.

    -Wes

    Kind of

    suppose the structures (unions) start off with a "type" or "switchcode" byte
    then you could do something like this

    Structure Structure1
      switchCode as uint8 
      rest(4) as uint8
    End Structure
    
    Structure Structure2
      switchCode as  uint8
      val1 as uint16
      val2 as uint16
    End Structure
    
    Structure Structure3
      switchCode as uint8 
      val1 as uint32
    End Structure
    
    
    
    Dim mb As new MemoryBlock(5)
    
    mb.Byte(0) = &h20
    mb.UInt16Value(1) = 32
    mb.UInt16Value(3) = 254
    
    Dim p As ptr = mb
    
    Dim s1 As structure1 =  p.structure1 // makes it so I can read the data in mb using the fields from structure1
    
    Select Case s1.switchcode
      
    Case &h20
      Dim s2 As structure2 =  p.structure2 // makes it so I can read the data in mb using the fields from structure2
      Dim value1 As UInt16 = s2.val1
      Dim value2 As UInt16 = s2.val2
      Break
      
    Case &h21
      Dim s3 As structure3 =  p.structure3 // makes it so I can read the data in mb using the fields from structure3
      Dim value1 As UInt32 = s3.val1
      Break
      
    End Select

or Sign Up to reply!