esp8266 / Basic

Basic Interpreter for the ESP8266
http://esp8266basic.com
259 stars 216 forks source link

NUL char cannot be part of a string? #43

Open gdelconti opened 6 years ago

gdelconti commented 6 years ago

See example below:

print "ESP Basic 3.0.Alpha 69"

' let define some control character let NUL = chr(0) 'required let SOH = chr(1) 'can be any let STX = chr(2) 'can be any

print "correct with any combitation of control char but the NUL" let s = SOH & STX print asc(left(s,1)) print asc(right(s,1))

print "wrong if NUL is part of the string" let s = NUL & STX print asc(left(s,1)) print asc(right(s,1))

oldbodd commented 6 years ago

Espbasic is a program written in a programming language created by people who thought it was a good idea to indicate the end of a string with a special character and picked chr(0) for that character. So when you put a chr(0) in a string, the program reads the string till it comes to that Chr(0) and treats it as end of string rather than a character with a value of 0 .. if you see what I mean.... I'm sure this has many advantages but strongly suspect that the phrase buffer overrun is not entirely unrelated to this type of thinking.... Some systems use two bytes to represent one....

gdelconti commented 6 years ago

I thought this was "C"-related. BTW, other people from different country in more recent times thought that using NUL char in Relay deactivation command was a good idea too ;-) http://www.smoke.com.au/museum/?p=379

Of course with ESPbasic there is an obvious workaround:

[R1OFF] ' A0 and A1 defined elsewhere serialprint chr(A0) serialprint chr(01) serialprint chr(00) serialprint chr(A1) return