Announcement

Collapse
No announcement yet.

UNION is a DREAM

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • UNION is a DREAM

    Let's try to understand how UNION works.
    I prepare a UNION with a fixed length variable 4 bytes long and an array of four bytes to access the single digits

    UNION Convert_ASCII
    StringVar AS STRING * 4
    Digits(4) AS BYTE
    END UNION

    if I want to PRINT the first char of the STRING, and I use the UNION structure (array)
    what I get? The ASCII code representation?
    How do I use the Digits() array?

    Can I just do something like:

    PRINT Digits(1);Digits(2)
    ?

    Thanks

  • #2
    Code:
    UNION Convert_ASCII
       StringVar AS STRING * 4
       Digits(1 TO 4) AS BYTE
    END UNION
    
    FUNCTION PBMAIN () AS LONG
    
       DIM ca AS Convert_ASCII
       ca.StringVar = "abcd"
       print CHR$(ca.digits(1))
       print CHR$(ca.digits(2))
       print CHR$(ca.digits(3))
       print CHR$(ca.digits(4))
       WAITKEY$
    
    END FUNCTION
    Alto notice that Digits(1 TO 4) AS BYTE is not the same as Digits(4) AS BYTE. The later creates an array with 5 elements, not 4, since arrays are 0 based.
    Forum: http://www.jose.it-berater.org/smfforum/index.php

    Comment


    • #3
      UNION is essentailly a TYPE.
      You need to create a variable of type Convert_ASCII
      '
      Code:
      #COMPILE EXE
      #DIM ALL
      
      UNION Convert_ASCII
          StringVar AS STRING * 4
          Digits(3) AS BYTE '4 bytes 0 to 3. Alternatively use Digits(1 to 4)
      END UNION
      
      FUNCTION PBMAIN() AS LONG
          LOCAL myVar AS Convert_ASCII
          LOCAL myString as STRING
          myString = "ABCD"
          myVar.StringVar = myString
          PRINT myVar.Digits(2)    ' returns 67 - the ASCII value of the third byte ("C")
          WAITKEY$
      END FUNCTION
      '
      Of course, it's probably easier just use the ASC() function
      Code:
      PRINT ASC(myString,3)

      Comment


      • #4
        Originally posted by José Roca View Post
        Code:
        UNION Convert_ASCII
        StringVar AS STRING * 4
        Digits(1 TO 4) AS BYTE
        END UNION
        
        FUNCTION PBMAIN () AS LONG
        
        DIM ca AS Convert_ASCII
        ca.StringVar = "abcd"
        print CHR$(ca.digits(1))
        print CHR$(ca.digits(2))
        print CHR$(ca.digits(3))
        print CHR$(ca.digits(4))
        WAITKEY$
        
        END FUNCTION
        Alto notice that Digits(1 TO 4) AS BYTE is not the same as Digits(4) AS BYTE. The later creates an array with 5 elements, not 4, since arrays are 0 based.
        Thanks José, Stuart. Very helpful

        Comment


        • #5
          Instead of messing with CHR$() and/or ASC(), why not let the UNION handle all of that? That is, why stop at two members?

          Code:
          UNION Foo
              S            AS STRING * 4
              B (1 to 4) AS BYTE
              C (1 TO 4) AS STRING * 1
          END UNION
          
                  LOCAL F AS Foo
                  TYPE SET F.S   = "1234"  
                   ?  F.B (2)                        ' print byte value (= ASC ("2") which I think is 49 decimal)
                   ?  F.C (2)                         ' print character value = "2"
          ...
          MCM




          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            The question is a bit vague... is this specifically about implementing unions? or are you just looking to address the characters or bytes in the string and saw unions as a means to an end?

            The union question was already answered but if you need specific characters you can simply use MID$; for byte values ASC was already mentioned as a PB intrinsic method, but you can also use pointers like this:


            Code:
            FUNCTION PBMAIN( ) AS LONG
                DIM v_sstrText AS STRING * 4
                DIM v_pbytBytes AS BYTE PTR
                v_sstrText = "abcd"
                v_pbytBytes = VARPTR( v_sstrText )
                PRINT @v_pbytBytes[ 0 ] ; @v_pbytBytes[ 3 ]
                WAITKEY$
            END FUNCTION
            <b>George W. Bleck</b>
            <img src='http://www.blecktech.com/myemail.gif'>

            Comment


            • #7
              Originally posted by George Bleck View Post
              The question is a bit vague... is this specifically about implementing unions? or are you just looking to address the characters or bytes in the string and saw unions as a means to an end?

              The union question was already answered but if you need specific characters you can simply use MID$; for byte values ASC was already mentioned as a PB intrinsic method, but you can also use pointers like this:


              Code:
              FUNCTION PBMAIN( ) AS LONG
              DIM v_sstrText AS STRING * 4
              DIM v_pbytBytes AS BYTE PTR
              v_sstrText = "abcd"
              v_pbytBytes = VARPTR( v_sstrText )
              PRINT @v_pbytBytes[ 0 ] ; @v_pbytBytes[ 3 ]
              WAITKEY$
              END FUNCTION
              Thanks George, I need all these information, because I'm studying PB syntax and possibilities right now.

              Comment

              Working...
              X