About Monkey 2 › Forums › Monkey 2 Programming Help › String to Void Ptr
Tagged: Void Ptr
This topic contains 5 replies, has 3 voices, and was last updated by
EdzUp
1 year, 8 months ago.
-
AuthorPosts
-
July 31, 2017 at 12:09 pm #9615
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?
July 31, 2017 at 3:57 pm #9617Are you shure you tried casting? Void Ptrs are made to avoid argument type error!
[/crayon]Monkey12[crayon-5cba87f5541b5251981301 inline="true" ]Local myVoidPtr:Void PtrmyVoidPtr=Cast<Void Ptr>(VarPtr myString)(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]Monkey12345[crayon-5cba87f5541bd917066996 inline="true" ]Local mydata:=New DataBuffer( str.CStringLength )str.ToCString( mydata.Data,mydata.Length )data.Data is your Void Ptr?
July 31, 2017 at 9:34 pm #9618will try casting like that I always thought it was Void Ptr (string) sort of like how C does it.
July 31, 2017 at 11:56 pm #9621Varptr 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 )August 1, 2017 at 7:27 am #9634Thanks guys I think I will go with the sockstream method as it’s for networking
August 2, 2017 at 7:32 am #9671I 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
-
AuthorPosts
You must be logged in to reply to this topic.