Announcement

Collapse
No announcement yet.

Simple conversion question + CHR$(1,0,1,0,0,0,0,0,0)

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

  • Simple conversion question + CHR$(1,0,1,0,0,0,0,0,0)

    This may look obvious.... But how do you get the individual values out of CHR$(1,0,1,0,0,0,0,0,0) ??

  • #2
    like this possibly?

    Code:
    #compile exe
    #dim all
    
    function pbmain () as long
    local i as long
    local s, ss as string
    
    s = chr$(11,12,13,14,15)
    
    for i = 1 to len(s)
        ss += str$(asc(mid$(s,i,1)))
    next
    
    ? ss
    
    end function

    Comment


    • #3
      ASC! I should have thought of that... I was lost trying to do it with pointers.

      Comment


      • #4
        or perhaps using a pointer:

        Code:
         #compile exe
        #dim all
        
        function pbmain () as long
        
            local mychrs, s as string
            local p as byte ptr
            local i, n as long
            mychrs = chr$(9,8,0,7,6)
            n = len(mychrs)
            p = strptr(mychrs)
            for i = 1 to n
                s += str$(@p) + " "
                incr p
            next
            ? s
        end function

        Comment


        • #5
          or using a byte array mapped at the strptr of the variable receiving the CHR$

          Code:
          #compile exe
          #dim all
          
          function pbmain () as long
          
              local mychrs, s  as string
              local p as byte ptr
              local chrvals() as byte
              local i, n as long
          
              mychrs = chr$(9,8,0,7,6)
              n = len(mychrs)
              dim chrvals(1 to n) at strptr(mychrs)
              for i = 1 to n
                  s += str$(chrvals(i)) + " "
              next
              ? s
          end function

          Comment


          • #6
            In your example, you only used 0 and 1. Do you use other values or is it always 0,1? If the latter you could look at BIT operations.

            Comment


            • #7
              Thanks everybody - good thoughts.

              Comment


              • #8
                You can "shortcut" the ASC method a bit by eliminating MID$:

                Code:
                FOR i = 1 TO LEN(s)
                    ss += STR$(ASC(s, i))
                NEXT
                Will also return what you're looking for.
                Adam Drake
                Drake Software

                Comment


                • #9
                  Nice Adam!
                  Thanks

                  Comment


                  • #10
                    CHR$ is also great for concatenating function results.
                    This function might be useful for extracting any byte.
                    Code:
                    FUNCTION PBMAIN () AS LONG
                     s$ = CHR$(9,8,0,7,6)
                     ? CHR$("First byte ",CHR$(PARSE$(GetASC(s$),1),$CR, _
                             "Last byte ",PARSE$(GetASC(s$),LEN(s$)),$CR, _
                             "All bytes ",GetASC(s$))),,"Done"
                    END FUNCTION
                    
                    FUNCTION GetASC(s AS STRING) AS STRING
                     LOCAL i,n AS LONG, sNew AS STRING
                     LOCAL sDelimiter AS STRING
                     sDelimiter = ","
                     n = LEN(s)
                     IF n = 0 THEN EXIT FUNCTION
                     DIM chrvals(1 TO n) AS BYTE AT STRPTR(s)
                     FOR i = 1 TO n
                      sNew += FORMAT$(chrvals(i)) + sDelimiter
                     NEXT
                     FUNCTION = LEFT$(sNew,-LEN(sDelimiter))
                    END FUNCTION
                    https://duckduckgo.com instead of google

                    Comment


                    • #11
                      Could not figure out how to get around using FORMAT$ or USING$
                      Code:
                      FUNCTION PBMAIN () AS LONG 'getasc.bas  4/16/19
                      
                       LOCAL s,sDelimiter AS STRING
                      
                       sDelimiter=","
                       s = CHR$(9,8,0,7,6)
                      
                       ? CHR$( v1(s),           $CR,_  'concatenate string
                               v2(s,sDelimiter),$CR,_  'join string
                               v3(s,sDelimiter),$CR,_  'stringbuilder
                               v4(s,sDelimiter))       'using
                      
                      END FUNCTION
                      
                      FUNCTION v1(s AS STRING) AS STRING
                       'concatenate string
                       LOCAL i,n AS LONG, sNew AS STRING
                       LOCAL sDelimiter AS STRING
                       sDelimiter = ","
                       n = LEN(s)
                       IF n = 0 THEN EXIT FUNCTION
                       DIM b(1 TO n) AS BYTE AT STRPTR(s)
                       FOR i = 1 TO n
                        sNew += FORMAT$(b(i)) + sDelimiter
                       NEXT
                       FUNCTION = LEFT$(sNew,-LEN(sDelimiter))
                      END FUNCTION
                      
                      FUNCTION v2(s AS STRING,sDelimiter AS STRING) AS STRING
                       'join array
                       LOCAL i,n AS LONG
                       n = LEN(s)
                       DIM bArray(1 TO n)AS BYTE AT STRPTR(s)
                       DIM sArray(1 TO n)AS STRING
                       FOR i = 1 TO n
                        sArray(i)=FORMAT$(bArray(i))
                       NEXT
                       FUNCTION=JOIN$(sArray(),sDelimiter)
                      END FUNCTION
                      
                      FUNCTION v3(s AS STRING,sDelimiter AS STRING) AS STRING
                       'stringbuilder
                       LOCAL i,n AS LONG
                       LOCAL sb AS ISTRINGBUILDERA
                       sb = CLASS  "STRINGBUILDERA"
                       n = LEN(s)
                       DIM bArray(1 TO n)AS BYTE AT STRPTR(s)
                       FOR i = 1 TO LEN(s)
                        sb.add FORMAT$(bArray(i))
                        sb.add sDelimiter
                       NEXT
                       FUNCTION=sb.string
                      END FUNCTION
                      
                      FUNCTION v4(s AS STRING,sDelimiter AS STRING) AS STRING
                       LOCAL sdel AS STRING
                       sdel = "#_"+sDelimiter
                       LOCAL i,n AS LONG
                       n = LEN(s)
                       DIM bArray(1 TO n)AS BYTE AT STRPTR(s)
                       DIM sArray(1 TO n)AS STRING
                       FOR i = 1 TO n
                        sArray(i)=USING$(sdel,bArray(i))
                       NEXT
                       FUNCTION=JOIN$(sArray(),"")
                      END FUNCTION
                      https://duckduckgo.com instead of google

                      Comment


                      • #12
                        Is "s = CHR$(9,8,0,7,6)" just some numbers you made up? Because that string is TAB, Backspace, NUL, BEL and Acknowledgement. Or, do you want to do something with characters "98076"?
                        Dale

                        Comment


                        • #13
                          Chris made them up, just values.
                          https://duckduckgo.com instead of google

                          Comment


                          • #14
                            In post 1 a string was made up of numbers of unknown purpose using CHR$(). Question was how to get the numbers back. ASC() is the answer because it is the "reverse" of CHR$().

                            No formatting for human readability comes into it.

                            Do you have a separate question about FORMAT$ or USING$?

                            Cheers,
                            Dale

                            Comment


                            • #15
                              No. Just looking for best way to get all ascii values into a delimited string.
                              Code:
                              LOCAL i AS LONG, s,s1 AS STRING
                              s = CHR$(9,8,0,7,6)
                              FOR i = 1 TO LEN(s)
                                s1+=USING$("#_,",ASC(s,i))
                              NEXT
                              ? LEFT$(s1,-1)
                              https://duckduckgo.com instead of google

                              Comment


                              • #16
                                Has anyone mentioned a parallel array of pointers?

                                It all depends on what you want to do with the data, how often, and how fast.

                                Code:
                                #compile exe
                                #dim all
                                #debug display
                                
                                function pbmain () as long
                                
                                    local s, s1 as string
                                    local i, n as long
                                    local pb() as byte ptr
                                    local ps as long
                                    '
                                    s = chr$("98076")
                                    n = len(s)
                                    dim pb(1 to n) as byte ptr
                                    ps = strptr(s)
                                    ' load parallel array
                                    for i = 1 to n
                                        pb(i) = ps
                                        incr ps
                                    next
                                    '....do stuff
                                    for i = 1 to n
                                        s1 += chr$(@pb(i))
                                    next
                                    ? s1
                                end function

                                Comment


                                • #17
                                  Chris,
                                  That one isn't returning ASC values (maybe because tested with "98076" instead of CHR$(9,8,0,7,6) ?
                                  Code:
                                  FUNCTION PBMAIN () AS LONG       'getasc.bas  4/16/19
                                   LOCAL i AS LONG, s,s1 AS STRING
                                   LOCAL sDelimiter AS STRING
                                   s = CHR$(9,8,0,7,6)
                                   sDelimiter=","
                                   ? CHR$( v1(s),           $CR,_  'concatenate string
                                           v2(s,sDelimiter),$CR,_  'join string
                                           v3(s,sDelimiter),$CR,_  'stringbuilder
                                           v4(s,sDelimiter),$CR,_  'using
                                           v5(s))                  'parallel array
                                  END FUNCTION
                                  
                                  FUNCTION v1(s AS STRING) AS STRING
                                   'concatenate string
                                   LOCAL i,n AS LONG, sNew AS STRING
                                   LOCAL sDelimiter AS STRING
                                   sDelimiter = ","
                                   n = LEN(s)
                                   IF n = 0 THEN EXIT FUNCTION
                                   DIM b(1 TO n) AS BYTE AT STRPTR(s)
                                   FOR i = 1 TO n
                                    sNew += FORMAT$(b(i)) + sDelimiter
                                   NEXT
                                   FUNCTION = LEFT$(sNew,-LEN(sDelimiter))
                                  END FUNCTION
                                  
                                  FUNCTION v2(s AS STRING,sDelimiter AS STRING) AS STRING
                                   'join array
                                   LOCAL i,n AS LONG
                                   n = LEN(s)
                                   DIM bArray(1 TO n)AS BYTE AT STRPTR(s)
                                   DIM sArray(1 TO n)AS STRING
                                   FOR i = 1 TO n
                                    sArray(i)=FORMAT$(bArray(i))
                                   NEXT
                                   FUNCTION=JOIN$(sArray(),sDelimiter)
                                  END FUNCTION
                                  
                                  FUNCTION v3(s AS STRING,sDelimiter AS STRING) AS STRING
                                   'stringbuilder
                                   LOCAL i,n AS LONG
                                   LOCAL sb AS ISTRINGBUILDERA
                                   sb = CLASS  "STRINGBUILDERA"
                                   n = LEN(s)
                                   DIM bArray(1 TO n)AS BYTE AT STRPTR(s)
                                   FOR i = 1 TO LEN(s)
                                    sb.add FORMAT$(bArray(i))
                                    sb.add sDelimiter
                                   NEXT
                                   FUNCTION=sb.string
                                  END FUNCTION
                                  
                                  FUNCTION v4(s AS STRING,sDelimiter AS STRING) AS STRING
                                   LOCAL sdel AS STRING
                                   sdel = "#_"+sDelimiter
                                   LOCAL i,n AS LONG
                                   n = LEN(s)
                                   DIM bArray(1 TO n)AS BYTE AT STRPTR(s)
                                   DIM sArray(1 TO n)AS STRING
                                   FOR i = 1 TO n
                                    sArray(i)=USING$(sdel,bArray(i))
                                   NEXT
                                   FUNCTION=JOIN$(sArray(),"")
                                  END FUNCTION
                                  
                                  FUNCTION v5(s AS STRING) AS STRING
                                  
                                   LOCAL s1 AS STRING
                                   LOCAL i, n AS LONG
                                   LOCAL pb() AS BYTE PTR
                                   LOCAL ps AS LONG
                                   '
                                   n = LEN(s)
                                   DIM pb(1 TO n) AS BYTE PTR
                                   ps = STRPTR(s)
                                   ' load parallel array
                                   FOR i = 1 TO n
                                    pb(i) = ps
                                    INCR ps
                                   NEXT
                                   '....do stuff
                                   FOR i = 1 TO n
                                    s1 += CHR$(@pb(i))
                                   NEXT
                                   FUNCTION = s1
                                  END FUNCTION
                                  https://duckduckgo.com instead of google

                                  Comment

                                  Working...
                                  X