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(iOffse t, 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.
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(
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.
can you paste your code here?
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.Le ngth) 'Prints: "4"
Console.WriteLine("------- ---")
Console.WriteLine(sTest.Su bstring(2, 3)) 'CRASH!
Console.WriteLine(sTest.Su bstring(2, 3).Length)
Dim sTest As String
sTest = ""
sTest += "X"
sTest += Chr(0)
sTest += "Y"
sTest += "Z"
Console.WriteLine(sTest) 'Prints: "X YZ"
Console.WriteLine(sTest.Le
Console.WriteLine("-------
Console.WriteLine(sTest.Su
Console.WriteLine(sTest.Su
ASKER
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.
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.
ASKER
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.Le ngth) 'Prints: "7"
Console.WriteLine("------- ---")
Console.WriteLine(sTest.Su bstring(3) ) 'Prints: "X YZ"
Console.WriteLine(sTest.Su bstring(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.
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.Le
Console.WriteLine("-------
Console.WriteLine(sTest.Su
Console.WriteLine(sTest.Su
------------------------
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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. :)
ASKER
"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.
VB.NET shouldn't treat one ASCII character differently than another one.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.