Link to home
Start Free TrialLog in
Avatar of cdwdirect
cdwdirect

asked on

In-line CHR(0) truncates a VB.NET String

I work with a lot of IEEE-compressed single-precision floating point data in fixed-length record file formats.  A field containing a compressed Single as stored in the file is four bytes of (normally) non-plaintext ASCII characters.  They often include one or more NULL-zero characters in any of the four byte positions.  The .NET framework seems to be behaving inconsistently when those four bytes are loaded into a string.

90% of the time a string is truncated at the first Chr(0) character in the character sequence. Yes... 90% of the time. It depends on the context. If you return the string BYVAL, for example, the .NET runtime cuts it down. However, if you use a StringBuilder and then convert and assign it to a string with StringBuilderVar.ToString() then Chr(0)'s do NOT cause the destination string to be truncated. But then StringVar.Substring(iOffset, iLen) DOES truncate the results if it contains a Chr(0)!

Can this behavior be avoided?  Is there a special version of the string variable that I can use that doesn't try and "fix" my string for me?  I would like to continue using strings if possible, since they are one of the best features of the BASIC language.  As it is, it is almost like I am coding in C because I'm having to do things one character at a time using character arrays.
Avatar of malharone
malharone

can you paste your code here?
Avatar of cdwdirect

ASKER

Here is a simplified example... paste into the Main() function of a Console app:

        Dim sTest As String

        sTest = ""
        sTest += "X"
        sTest += Chr(0)
        sTest += "Y"
        sTest += "Z"

        Console.WriteLine(sTest)                'Prints: "X YZ"
        Console.WriteLine(sTest.Length)         'Prints: "4"
        Console.WriteLine("----------")
        Console.WriteLine(sTest.Substring(2, 3))    'CRASH!
        Console.WriteLine(sTest.Substring(2, 3).Length)
I thought Null-zero terminated strings were a simplistic convention perfect for the C language's tight constraints.

They seem very out of place in BASIC, especially VB.NET, and the inconsistency of their effect is maddening.

Notice how the initial assignment it worked fine?  Returning a string BYVAL from a function seems to work fine too, sometimes, but the sTest.Substring() method chops the end off.
A contrasting example...  Paste into the Main() function of a Console app:

        Dim sTest As String

        sTest = ""
        sTest += "1"
        sTest += "2"
        sTest += "3"
        sTest += "X"
        sTest += Chr(0)
        sTest += "Y"
        sTest += "Z"

        Console.WriteLine(sTest)                'Prints: "123X YZ"
        Console.WriteLine(sTest.Length)         'Prints: "7"
        Console.WriteLine("----------")
        Console.WriteLine(sTest.Substring(3))   'Prints: "X YZ"
        Console.WriteLine(sTest.Substring(3).Length) 'Prints: "4"

------------------------

So if the result of sTest.Substring() does not BEGIN with a Chr(0) then you're safe.  Mid() demonstrates the same failing behavior, FYI.
SOLUTION
Avatar of malharone
malharone

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Since many many many of the strings I'd be working with begin with a Chr(0) on purpose... the question is still VERY open.  :)
"Nothing" is a special keyword in VB.NET that dereferences an object.  Chr(0) is an ASCII character, it's REAL DATA, just like Chr(1) through Chr(255)...

VB.NET shouldn't treat one ASCII character differently than another one.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial