String to Void Ptr

About Monkey 2 Forums Monkey 2 Programming Help String to Void Ptr

Tagged: 

This topic contains 5 replies, has 3 voices, and was last updated by  EdzUp 1 year, 8 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #9615

    EdzUp
    Participant

    Having not used pointers much in Monkey or Blitz land I tend to leave that side of things to C++ but Socket.Send requires a Void Ptr for the data argument.

    My question is how do I get a string to be sent through Socket.Send so I can sort it at the other end for processing, I have the packet made up and ready just I get the  Can’t find overload for ‘Send:Int(Void Ptr,Int)’ with argument types (String,Int) when trying to send it.

    Ive tried casting, databuffers etc but to no avail. Its probably something stupidly simple but it eludes me.

    Anyone have any ideas?

    #9617

    abakobo
    Participant

    Are you shure you tried casting? Void Ptrs are made to avoid argument type error!

    [/crayon]

    (not tested)

    To me the problem is more the conversion to CString as Ptrs most often mean external stuff..

    from string docs:

    Method ToCString:Void( buf:Void Ptr, bufSize:Int )
    Converts the string to a CString.

    If there is enough room in the memory buffer, a null terminating ‘0’ is appended to the CString.

    [/crayon]

    data.Data is your Void Ptr?

    #9618

    EdzUp
    Participant

    will try casting like that I always thought it was Void Ptr (string) sort of like how C does it.

    #9621

    Mark Sibly
    Keymaster

    Varptr str wont work as a string is itself a pointer so you end up to a pointer to a pointer. Not only that, strings are 16 bit encoded and prefixed with a length int.

    This is the better approach:

    Local mydata:=New DataBuffer( str.CStringLength )
    str.ToCString( mydata.Data,mydata.Length )

    Or you can create a ‘SocketStream’ eg:

    Local stream:=New SocketStream( socket )
    stream.WriteCString( str )

    #9634

    EdzUp
    Participant

    Thanks guys I think I will go with the sockstream method as it’s for networking 🙂

    #9671

    EdzUp
    Participant

    I had a look at SocketStreams and found it has a SendString method I don’t know how I could have missed that. It all compiles now so it’s onto testing to see what I get from the streams the other end 🙂

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.