Page 1 of 2

Allow Ascii character 0 in strings

Posted: Thu Oct 02, 2008 10:07 pm
by EdzUp[SD]
Hi,
the main reason for this is to allow conversion from one type to another.

For instance take long int value 5, converted to a four byte value it would become, chr(0)+chr(0)+chr(0)+chr(5). When return from a bank to the string all we get is chr(5).

The reason I ask this is compression for networking is simpler if you can take a value in the billions and convert it to four bytes for sending over a network this is also true for floats. It seems some characters are ignored by strings in PB, the main characters PB needs to act upon is simply chr(10) and chr(13) nothing more the rest should be sent to the application for use.

Using Val/Str/Hex it takes eight bytes to represent the longint where if converted it would be four in networking this is a waste of bandwidth and on a MMO it could result in lag.

Would this be possible?

Posted: Thu Oct 02, 2008 10:20 pm
by Kaeru Gaman
*emptied*

Posted: Thu Oct 02, 2008 10:21 pm
by Fred
I don't understand, why do you want to use string when sending binary data ?

Kearu: no need to be harsh

Posted: Thu Oct 02, 2008 10:26 pm
by Kaeru Gaman
I'm sorry, seemed to be the drop to let the barrel overflow...
edited my above post.

Posted: Thu Oct 02, 2008 10:43 pm
by Trond
Kaeru Gaman wrote:I'm sorry, seemed to be the drop to let the barrel overflow...
edited my above post.
I can totally see the need for that... :wink:

EdzUp, there is no technical difference between sending a long/float (4 bytes) directly and sending it as a string. In both cases 4 bytes of binary data will be sent over the connection.

Here is how to send the data directly:

Code: Select all

Global Longvariable.l
Global Floatvariable.f
Global DoubleVariable.d

SendNetworkData(ClientID, @Longvariable, 4)
SendNetworkData(ClientID, @FloatVariable, 4)
SendNetworkData(ClientID, @DoubleVariable, 8)
Reading the bytes from the variable, putting the bytes into a string and then sending the same bytes (just read from the string instead of from the original variables) is much slower.

Posted: Fri Oct 03, 2008 7:35 am
by blueznl
Yep, the question seems to be: why using a string to communicate? Why not directly send the data itself?

Posted: Fri Oct 03, 2008 7:57 am
by Lykaestria
blueznl wrote:Yep, the question seems to be: why using a string to communicate? Why not directly send the data itself?
I think I can answer that, and I know where the author of this thread is coming from, because I tried the very same thing myself.

My idea was to send all types of data for a multiplayer application in a single line, with the server just quickly parroting the data back to all the other clients but the one who sent it, whereupon the clients filter the string to the right handling routine based upon the first character stored in the string.

Alas this didn't work because most ascii characters below value 32 were not being recognised... so I now have to do it the long way with individual byte, float, word, long, and string buffers for the server to filter out and then resend the data on it's way :wink:

Posted: Fri Oct 03, 2008 8:11 am
by EdzUp[SD]
Simple image a packet system where you need to send:
ClientID[longint]
ClientFrom[longint]
Message Class[longint]
MessageID[Longint]
important[1byte]
Message[random length]

Now if you sent it as you suggest it would come through in random integers so there is no way of knowing who its from. If there was an adjustment to the string it a new 'raw' data type it would be easier to send network data.

Re: Allow Ascii character 0 in strings

Posted: Fri Oct 03, 2008 9:27 am
by PB
Strings in PureBasic are 0-byte terminated, just like C. This is not going to
change, as stated by both Fred and Freak in the hundreds of requests just
like this in the past. Get over it. Do a search and you'll see their reasons.

Posted: Fri Oct 03, 2008 10:00 am
by Trond
EdzUp[SD] wrote:Now if you sent it as you suggest it would come through in random integers so there is no way of knowing who its from.
If you send it as a string it will still be recieved as random integers. You can find out who it's from by checking EventClient() or packing the data into a structure and sending it all at once:

Code: Select all

Structure Package
  SenderID.l
  Message.l
EndStructure

Var.Package\SendID = 1234
Var\Message = 9876
SendNetworkData(ConnectionID, @Var, SizeOf(Package))

Posted: Fri Oct 03, 2008 10:04 am
by EdzUp[SD]
hmm ok, will check it out just thinking of fuctions returning strings too.

Posted: Fri Oct 03, 2008 10:32 am
by Kaeru Gaman
Procedures always return a Register.
this CAN be used as a pointer to a String, but it also could be used as a pointer to a not-null-terminated buffer.

just get familiar with Allocated Memory, fixedlength Buffers, etc.
there is no need to and no gain in messing around with strings for non-string issues.

Posted: Fri Oct 03, 2008 12:05 pm
by EdzUp[SD]
Thanks Trond didnt think you could do that, will definately give it a go.

Coming from BlitzMax that allows bytes in strings its big learning curve getting to know PB but it does seem very flexible :)

Posted: Fri Oct 03, 2008 12:10 pm
by Trond
In PB it's best practice to only use strings for text data. Else you will have endless problems.

Posted: Fri Oct 03, 2008 2:53 pm
by EdzUp[SD]
Does the data come through in order or will I have to sort that out?