Allow Ascii character 0 in strings

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
EdzUp[SD]
Enthusiast
Enthusiast
Posts: 104
Joined: Thu Jun 26, 2008 10:53 pm
Location: Banstead, UK

Allow Ascii character 0 in strings

Post 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?
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

*emptied*
Last edited by Kaeru Gaman on Thu Oct 02, 2008 10:26 pm, edited 1 time in total.
oh... and have a nice day.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

I don't understand, why do you want to use string when sending binary data ?

Kearu: no need to be harsh
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

I'm sorry, seemed to be the drop to let the barrel overflow...
edited my above post.
oh... and have a nice day.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

Yep, the question seems to be: why using a string to communicate? Why not directly send the data itself?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Lykaestria
User
User
Posts: 76
Joined: Wed Sep 17, 2008 3:10 am
Location: New Zealand

Post 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:
EdzUp[SD]
Enthusiast
Enthusiast
Posts: 104
Joined: Thu Jun 26, 2008 10:53 pm
Location: Banstead, UK

Post 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.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Allow Ascii character 0 in strings

Post 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.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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))
EdzUp[SD]
Enthusiast
Enthusiast
Posts: 104
Joined: Thu Jun 26, 2008 10:53 pm
Location: Banstead, UK

Post by EdzUp[SD] »

hmm ok, will check it out just thinking of fuctions returning strings too.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
EdzUp[SD]
Enthusiast
Enthusiast
Posts: 104
Joined: Thu Jun 26, 2008 10:53 pm
Location: Banstead, UK

Post 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 :)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

In PB it's best practice to only use strings for text data. Else you will have endless problems.
EdzUp[SD]
Enthusiast
Enthusiast
Posts: 104
Joined: Thu Jun 26, 2008 10:53 pm
Location: Banstead, UK

Post by EdzUp[SD] »

Does the data come through in order or will I have to sort that out?
Post Reply