Menu

#753 Assigning a fixed-length string stops to copy at the first appearance of chr(0) and the rest is ignored

open
nobody
None
compiler
2024-04-11
2014-11-21
No

By assigning a fixed-length string, the copying unexpectedly stops at the first appearance of chr(0) in the fixed-length string and the rest is ignored.

Example:

Dim s1 As String * 10, s2 As String * 15, s As String

Mid(S1, 1) = "0"
Mid(s1, 10) = "9"
Print "'"; : Print s1; : Print "'"
Print

s2 = s1
Print "'"; : Print s2; : Print "'"
s = s1
Print "'"; : Print s; : Print "'"

Sleep

Output:

'0        9'

'0              '
'0'

A workaround is to use the substring function Mid(), even without explicitly pass the substring length as parameter:

Dim s1 As String * 10, s2 As String * 15, s As String

Mid(S1, 1) = "0"
Mid(s1, 10) = "9"
Print "'"; : Print s1; : Print "'"
Print

s2 = s1
Print "'"; : Print s2; : Print "'"
s = s1
Print "'"; : Print s; : Print "'"
Print

s2 = Mid(s1, 1)
Print "'"; : Print s2; : Print "'"
s = Mid(s1, 1)
Print "'"; : Print s; : Print "'"

Sleep

Output:

'0        9'

'0              '
'0'

'0        9     '
'0        9'

Therefore the first behavior (diect assignment) could probably be improved.


Referring to forum, from the post:
http://www.freebasic.net/forum/viewtopic.php?p=202720#p202720

Related

Bugs: #966

Discussion

  • fxm (freebasic.net)

    Two other different behaviors with a fixed-length string:

    1) declaration with initializer (with literals & Chr(0))
    'Chr(0)' is taken into account and the following characters:

    Dim As String * 10 s = "1234" & Chr(0) & "5678"
    Print s[4]
    Print s
    
    Sleep
    

    Output:

    0
    1234 5678
    

    2) assignment (with literals & Chr(0))
    'Chr(0)' is skipped but not the following characters:

    Dim As String * 10 s
    s = "1234" & Chr(0) & "5678"
    Print s[4]
    Print s
    
    Sleep
    

    Output:

    53
    12345678
    
     
  • fxm (freebasic.net)

    The new definition of the 'STRING*N' datatype (since fbc 1.20.0) solves the problem.

     

Log in to post a comment.

Monday.com Logo