BASIC

Author Topic: Detecting end of string character  (Read 2231 times)

Offline FutureCow

  • HelpEditor
  • Prof. Inline
  • ******
  • Posts: 680
    • View Profile
Detecting end of string character
« on: 2010-Feb-02 »
This is just for interest reasons. I'm wondering why the following doesn't work (assuming all you typed was the enter key)
Code: GLBasic [Select]
input string$,0,0
if mid$(string$,0,1) = CHR$(0)
...

but the following will work
Code: GLBasic [Select]
input string$,0,0
if mid$(string$,0,1) = "\0"

The second option is fine for my code, but I'm curious as to why the first one won't work as well.

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1772
    • View Profile
    • Homepage
Re: Detecting end of string character
« Reply #1 on: 2010-Feb-02 »
Don't rely on zero terminated strings in basic :-)

Offline FutureCow

  • HelpEditor
  • Prof. Inline
  • ******
  • Posts: 680
    • View Profile
Re: Detecting end of string character
« Reply #2 on: 2010-Feb-02 »
I'm not relying on it, but my text input routine is checking for it as an end of string character. Why does one command pick it up but the other one not?

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1772
    • View Profile
    • Homepage
Re: Detecting end of string character
« Reply #3 on: 2010-Feb-02 »
Mabe you are testing behind the end of the string and it just happens to be a zero there at the moment?

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10689
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: Detecting end of string character
« Reply #4 on: 2010-Feb-02 »
the ="\0" will compare to an empty string.

Offline FutureCow

  • HelpEditor
  • Prof. Inline
  • ******
  • Posts: 680
    • View Profile
Re: Detecting end of string character
« Reply #5 on: 2010-Feb-02 »
Sorry Gernot, I don't understand what you mean. The "\0" is being added by the 'INPUT' command when you hit the enter key.

Here's some test code.
Try it as is, then swap the comment on these two lines. When the comparrison is "CHR$(0)" the \0 character isn't detected and the code thinks you've typed an invalid character, but it works if you change it to check for '\0'

These are the lines to swap the comment on
Code: GLBasic [Select]
                IF Char$ = CHR$(0)
//              IF Char$ = "\0"

Test code
Code: GLBasic [Select]
TestFunc()

FUNCTION TestFunc:
        LOCAL FinalString$
        LOCAL MyString$
        LOCAL Char$
        LOCAL Loop
       
        PRINT "Type in a string",0,0
        INPUT MyString$,0,50
        FOR Loop = 0 TO LEN(MyString$)
                Char$=MID$(MyString$,Loop,1)
                IF Char$ = CHR$(0)
//              IF Char$ = "\0"
                        PRINT "\\0 found",0,150
                        SHOWSCREEN
                        SLEEP 2000
                        RETURN 2
                ELSEIF Char$ < "0" OR Char$ > "9"
                        PRINT "Error - illegal character " + Char$ + " found at position " + (Loop+1) + ". Only 0-9 accepted.\n",0,100
                        PRINT "ASC = " + ASC(Char$) + "\n",0,200
                        SHOWSCREEN
                        SLEEP 2000
                        RETURN 1
                ELSE
                        FinalString$=FinalString$ + Char$
                ENDIF
        NEXT
        PRINT "Final String = "+FinalString$,0,200
        SHOWSCREEN
        SLEEP 2000
ENDFUNCTION

 

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10689
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: Detecting end of string character
« Reply #6 on: 2010-Feb-03 »
   FOR Loop = 0 TO LEN(MyString$)


must be LEN(MyString$) - 1. You have a string:
"012", length=3. Character at position 3 = '\0', but that's just to be compatible with C APIs.

Offline FutureCow

  • HelpEditor
  • Prof. Inline
  • ******
  • Posts: 680
    • View Profile
Re: Detecting end of string character
« Reply #7 on: 2010-Feb-03 »
All good - but isn't '\0' the same as CHR$(0)? That was why I was confused - I thought both would have detected the NUL character.

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10689
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: Detecting end of string character
« Reply #8 on: 2010-Feb-03 »
OK.
INPUT does not append a chr$(0) to your string. Internally there is this character to ensure you can use the string pointer as a C string as it is required in many C functions (sprintf e.g.).
On the GLBasic side of the code, there is no "\0" character. The string "012" is 3 characters long "0", "1" and "2". That's it. Accessing anything beyond that will not work.
Now, MID$("012", 3, 1) will return "" (empty string), and if you compare that to chr$("\0"), the C function strcmp will return 0 (equal) because both strings are 0 length and thus equal.
It might be bad to use strcmp in the first place, because chr$(0) actually is a binary "string" that really is 1 character long - my fault here.

Could you follow this far?

Back to your problem: INPUT does not append any newline character. If you press the enter key, INKEY$() will return "\n", though. But not INPUT.

What are you trying to achieve in detail?

Hatonastick

  • Guest
Re: Detecting end of string character
« Reply #9 on: 2010-Feb-03 »
I'd written a program myself to work out what was going on and had just written a long reply when the forum system informed me there were two replies.  Reading Gernots made my post completely moot.  Oh well.  It was a fun exercise all the same. :)

I'm guessing it relates to this post?
http://www.glbasic.com/forum/index.php?topic=4086.0
« Last Edit: 2010-Feb-03 by Hatonastick »

Offline FutureCow

  • HelpEditor
  • Prof. Inline
  • ******
  • Posts: 680
    • View Profile
Re: Detecting end of string character
« Reply #10 on: 2010-Feb-04 »
Thanks Gernot (and Hatonastick!) That explaination makes my issue make a lot more sense now.

What I was trying to achieve : For my boardgame's network mode I wrote a little function to parse an IP address you type in. The input is received from the user using the "INPUT" command (it's not the way I wanted to do it as it stops the rest of the program while it waits for input, but it was quicker to implement than writing my own function with INKEY$() and I needed something in a hurry - the deadline for the competition is close after all!  =D).

My code does a character by character check to make sure that the string you typed in is in the right format (a.b.c.d), that you have only typed in numbers for a/b/c/d, and that a/b/c/d are in the range 0-255. It loops across each of the characters using "LEN(MyString$)" so it was reading a '\0' character at the end. I'd tried searching for '\n' thinking that was the character that wasn't matching my comparrisons (0-9 or '.') - but eventually found it was the '\0' character.

As I'd originally tried checking if the character was a 'CHR$(0)' character and that didn't work, I was wondering why it didn't work but a check for '\0' did as they should effectively be the same thing.

It's all good now, I just wanted to know why there was a difference between the two. Is this likely to change (ie. are you likely to change from using strcmp?) as otherwise I might add something to the online notes about it.

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1772
    • View Profile
    • Homepage
Re: Detecting end of string character
« Reply #11 on: 2010-Feb-04 »
Strings in basic is never supposed to be zero-terminated so I think you can safely add something to the online notes that comparing strings with chr$(zero) in them is not recommended in GLBasic :-)

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10689
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: Detecting end of string character
« Reply #12 on: 2010-Feb-04 »
OK, just a few suggestions:

1) Use Splitstr(inp$, array$[], "."); IF LEN(array$[]) <> 4 THEN erro("wrong IP"); then parse the 4 ip tokens.
2) Ignore any characters, just connect. You can connect to "PC-GF", since that resolves to 192.168.178.23 on my LAN.
3) Use DDGui for input:
     ip$ = DDgui_input$(ip$, FALSE);
   
or make your own dialog (if you don't want the keyboard)
Code: GLBasic [Select]
DDgui_pushdialog(0,0,400,400)
ddgui_widget("", "Enter the IP")
ddgui_text("tx_ip", ip_default$, 300)
ddgui_button("bt_ok", "OK")
while true
   ddgui_show()
   showscreen
   if ddgui_get("bt_ok", "CLICKED") then break
wend

the_ip$ = ddgui_get("tx_ip", "TEXT")
 

Really, DDgui as ugly as it may seem, is very easy to use.


Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1772
    • View Profile
    • Homepage
Re: Detecting end of string character
« Reply #13 on: 2010-Feb-04 »
I'm using DDGui for just this reason in one of my network tests. Possible Scribble. I just allow anything since the connect string can be either an IP-number, domain address or just a computer name on your local lan. Or localhost for testing on the same computer.