Announcement

Collapse
No announcement yet.

converting between strings and byte arrays

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

  • Paul Dwyer
    replied
    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)

    Leave a comment:


  • Eric Pearson
    replied
    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>

    Leave a comment:


  • Paul Dwyer
    replied
    ooooh,

    that's a clever idea,

    I'll give that a go. Thanks

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

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

    Leave a comment:


  • John Kovacich
    replied
    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

    Leave a comment:


  • Eric Pearson
    replied
    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).]

    Leave a comment:


  • Paul Dwyer
    started a topic converting between strings and byte arrays

    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)
Working...
X
😀
🥰
🤢
😎
😡
👍
👎