Announcement

Collapse
No announcement yet.

converting between strings and byte arrays

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

  • converting between strings and byte arrays

    I saw some PB code to dim an array using the AT keyword to point it to a string and have access to the data through the array, which I thought was great. is there a way to reverse the process without a loop?

    I have the data in a byte array and I want to put it in a string but I would like to just pass a pointer or something.

    can you do something like

    Dim Strg as String
    Strg = VarPtr(MyArray(0))

    or something ???????

    or maybe:

    Dim StrgPtr as string Ptr

    StrgPtr = VarPtr(MyArray(0))

    then use @StrgPtr ????????? NFI !


    or should I use memcopy in a function?


    ------------------

    Paul Dwyer
    Network Engineer
    Aussie in Tokyo
    (Paul282 at VB-World)

  • #2
    Paul --

    It may be possible to do what you're describing, I've never tried. I usually do this...

    Code:
    sString = PEEK$(VARPTR(MyArray(x)), y)
    ...where x is the lowest-numbered element of the array, and y is the number of elements times the size of each one (4 for a LONG, etc.).

    That won't actually DIM a string at the desired location, but it will allow you to copy the contents of the numeric array into a string "image" of the array data.

    -- Eric

    ------------------
    Perfect Sync: Perfect Sync Development Tools
    Email: mailto:[email protected][email protected]</A>

    [This message has been edited by Eric Pearson (edited February 05, 2001).]
    "Not my circus, not my monkeys."

    Comment


    • #3
      If you dim the array AT the address of the string BEFORE you populate either of them,
      wouldn't it work in both directions anyway? You fill the array and read it as
      a string, or vice versa?



      ------------------
      Thanks,

      John Kovacich
      Thanks,

      John Kovacich
      Ivory Tower Software

      Comment


      • #4
        ooooh,

        that's a clever idea,

        I'll give that a go. Thanks

        ------------------

        Paul Dwyer
        Network Engineer
        Aussie in Tokyo
        (Paul282 at VB-World)

        Comment


        • #5
          Keep in mind that variable-length strings move when you change their value. Use LSET to assign a value to a string if you want it to stay put.

          -- Eric


          ------------------
          Perfect Sync: Perfect Sync Development Tools
          Email: mailto:[email protected][email protected]</A>
          "Not my circus, not my monkeys."

          Comment


          • #6
            Thank you all,

            Good point Eric, I think at first I'll get into the habbit of checking the pointer address before writing and reading to them.

            Code:
            #Compile Exe
            
            Function PbMain()
                Dim dstr As String
                
                dstr = "Hello"
                
                Dim MyArray(Len(dstr)) As Byte At StrPtr(dstr)
                
                MyArray(0) = Asc("A")
                MsgBox dstr
            
            End Function
            This works fine but if you move the MyArray dim above the "Hello" then the address changes after that and the msgbox returns "Hello" instead of "Aello"

            Cheers all

            ------------------

            Paul Dwyer
            Network Engineer
            Aussie in Tokyo
            (Paul282 at VB-World)

            Comment

            Working...
            X
            😀
            🥰
            🤢
            😎
            😡
            👍
            👎