{"id":186,"date":"2019-07-16T22:18:27","date_gmt":"2019-07-17T04:18:27","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=186"},"modified":"2019-07-13T22:28:09","modified_gmt":"2019-07-14T04:28:09","slug":"c-unions","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2019\/07\/16\/c-unions\/","title":{"rendered":"C Unions"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">An interesting question came up on the forums the other day about how to mimic &#8220;Unions&#8221; from C.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Turns out I had answered this some time ago &#8211; its close but not exactly the same as in C. But it is  close enough to be functional and close enough for most uses.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And it&#8217;s not mentioned anywhere in the Xojo docs. Not even in advanced topics.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The requirement was a need to be able to interpret a set of data (4 bytes in the forums post case) in one of two different ways. One was as 4 separate byte values, and the other as 2 16 bit values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the example I had given previously the need was a common &#8220;record&#8221; type that had an identifying &#8220;record type&#8221; byte as the first byte and several other interpretations of the data that followed. In all cases the records were the same total size but their contents varied.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the example I have the three structures following :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Structure Structure1\n  switchCode as uint8 \n  rest(4) as uint8\nEnd Structure\n\nStructure Structure2\n  switchCode as  uint8\n  val1 as uint16\n  val2 as uint16\nEnd Structure\n\nStructure Structure3\n  switchCode as uint8 \n  val1 as uint32\nEnd Structure<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Note that each has, in this set up, a byte at the beginning that is at a common offset and that is  used to determine which of the three structures is the correct one to be using to interpret the data. In some cases there may be some other indicator or mechanism to know which  way to interpret the data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this example all structures are defined to be the same total size. This is NOT required. A C union will be the largest of any of the defined union members. So make sure you account for this when you decide what memoryblock size to use for the initial data buffer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The data buffer is just a memoryblock of whatever size is needed. To interpret the data differently a Ptr is used. And since Ptr&#8217;s can interpret the data they point at via a structure we can, once the mb is assigned to the Ptr, now interpret the data using any of our defined structures.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Dim mb As new MemoryBlock(5)\n\nmb.Byte(0) = &amp;h20\nmb.UInt16Value(1) = 32\nmb.UInt16Value(3) = 254\n\nDim p As ptr = mb\n\nDim s1 As structure1 =  p.structure1 \/\/ makes it so I can read the\n            \/\/ data in mb using the fields from structure1\n            \/\/ but I could use any of the three OR \n            \/\/ some other mechanism to figure out which structure\n            \/\/ to use to inspect the data\n\nSelect Case s1.switchcode\n\nCase &amp;h20\n  Dim s2 As structure2 =  p.structure2 \n\/\/ makes it so I can read the data in mb using \n\/\/ the fields from structure2\n\n  Dim value1 As UInt16 = s2.val1\n  Dim value2 As UInt16 = s2.val2\n  Break\n\nCase &amp;h21\n  Dim s3 As structure3 =  p.structure3 \n\/\/ makes it so I can read the data in mb using \n\/\/ the fields from structure3\n\n  Dim value1 As UInt32 = s3.val1\n  Break\n\nCase &amp;h22  \n  Dim struct1 As structure1 =  p.structure1 \n\/\/ makes it so I can read the data in mb using \n\/\/ the fields from structure1\n\n  Dim value1 As UInt8 = struct1.rest(0)\n  Dim value2 As UInt8 = struct1.rest(1)\n  Dim value3 As UInt8 = struct1.rest(2)\n  Dim value4 As UInt8 = struct1.rest(3)\n\n  Break\n\nEnd Select<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Using this technique we can &#8220;overlay&#8221; the structure onto the raw data in the memoryblock and read it out using the structures fields.<br> Very handy especially for those cases where you need to read from file formats, memory formats that involve C unions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An interesting question came up on the forums the other day about how to mimic &#8220;Unions&#8221; from C. Turns out I had answered this some time ago &#8211; its close but not exactly the same as in C. But it is close enough to be functional and close enough for most uses. And it&#8217;s not &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2019\/07\/16\/c-unions\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;C Unions&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[1],"tags":[27,3],"class_list":["post-186","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-advanced-techniques","tag-xojo"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/comments?post=186"}],"version-history":[{"count":1,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/186\/revisions"}],"predecessor-version":[{"id":187,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/186\/revisions\/187"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}