No announcement yet.

STRFMT - Dave Navaro example

  • Filter
  • Time
  • Show
Clear All
new posts

  • STRFMT - Dave Navaro example

    hi dave and all,

    i was just testing your example code with small modifications as below.

    not that the formatting of the single is a lot of digits, unless
    i use a format string. the integer seems to be shown in hex.

    maybe we should include formatting strings in the function?

    has anyone tried this?

    thanks david
    'source dave navarro
    ' [url=""][/url] 
    'c/c++ has wsprintf() which will create a string from different parts (much like the old 'using$ in pb/dos).
    'strfmt$ is similar, allowing you to use templates like
    '"this is a %s - %l, %l, %b, %b"
    '%s -- string
    '%i -- integer
    '%l -- long integer
    '%q -- quad integer
    '%b -- byte
    '%w -- word
    '%d -- dword
    '%f -- single (float)
    '%2 -- double
    '%e -- extended precision float
    '%h -- hexedecimal (long or dword)
    #compile exe
    '#debug error on
    #dim all
    #register none
    #include ""
    '#include ""
    union datatype
       i as integer    '%i
       l as long       '%l
       q as quad       '%q
       b as byte       '%b
       w as word       '%w
       d as dword      '%d and %h
       f as single     '%f
       f2 as double    '%2
       e as ext        '%e
       s as long       '%s (string handle)
    end union
    function strfmt cdecl (byval f as string, any [, any, any, any, any, any, any, any, any]) as string
      local x as long
      local p as datatype ptr
      local s as string ptr
      local b as byte
      local t as string
      p = varptr(f)
      for x = 1 to len(f)
        b = asc(f, x)
        if b = 37 then
          incr x
          b = asc(f, x)
          if b = 37 then
            t = t & chr$(b)
            p = p + 4
            select case b
              case  50  'float (double)
                t = t & format$(@@p.f2)
              case  98  'byte
                t = t & format$(@@p.b)
              case 100  'dword
                t = t & format$(@@p.d)
              case 101  'float (extended)
                t = t & format$(@@p.e)
              case 102  'float (single)
                ' t = t & format$(@@p.f,"0.00") 'dlm testing
                t = t & format$(@@p.f)
              case 105  'hex (long or dword)
                t = t & hex$(@@p.d)
              case 105  'integer
                t = t & format$(@@p.i)
              case 108  'long
                t = t & format$(@@p.l)
              case 113  'quad
                t = t & format$(@@p.q)
              case 115  'string
                s = @p.s
                t = t & @s
              case 119  'word
                t = t & format$(@@p.l)
            end select
          end if
          t = t & chr$(b)
        end if
      function = t
    end function
    function pbmain()
       local a$, b$,c?,d%,e&,f!,strc$
      a$ = "this is a %s -- %b, %i, %l, %f"
      b$ = "test"
      c? = 1
      d% = 16
      e& = 3500500
      f! = 4.34!
      strc$ = strfmt(a$, b$, c?, d%, e&, f!)
      msgbox a$
      msgbox strc$
      'print a$
      'print strc$
    end function
    'home of the basic gurus