VB.NET Fixed String with embedded null characters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JKAT
    New Member
    • Apr 2008
    • 2

    VB.NET Fixed String with embedded null characters

    Hi All,

    I am using a com object to maniplate data inside an external program using VB.NET. One of the methods this com object provides requires 1 string parameter.

    The external program, however, requires a sting of a specified length, regardless of the number of characters used. The string to be passed to the function (length 36) is a combination of the below key fields.

    keyfield 1 length = 2
    keyfield 2 length = 7
    keyfield 3 length = 10
    keyfield 4 length = 6
    keyfield 5 length = 14

    If the actual value assigned to keyfield 2 has a length less than 7, it should be padded to the right with null characters.

    If the actual value assigned to keyfield 3 has a length less than 10, it should be padded to the right with null characters.

    If I have the following values:

    1: "00"
    2: "11111"
    3. "123"
    4: "XXXXXX"
    5: "YYYYYYYYYYYYYY "

    I would need the actual values :

    keyfield 1 value = "00"
    keyfield 2 value = "11111" + chr(0) + chr(0)
    keyfield 3 value = "123" + chr(0) + chr(0) + chr(0) + chr(0) + chr(0) + chr(0) + chr(0)
    keyfield 4 value= "XXXXXX"
    keyfield 5 value = "YYYYYYYYYYYYYY "

    Which, when concatenated, would result in the below key to be passed to com object method:
    "0011111 123 XXXXXXYYYYYYYYY YYYYY"

    Using a standard .NET string, the first null character (chr(0)) is interpreted as a terminator and the balance of the string is gone.

    I have tried the VB6 compatability's VBFixedString type but the com object's method throws an exception due to a type mismatch.

    If I convert the VBFixedString back to a .NET string before passing it to the method, the first null character is again interpreted as a string terminator and the rest of my string is chopped.

    Does anyone know if there is a way to get around this problem? Any help would be greatly appreciated.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Well, in general (C#, C++, Java, C etc etc) Strings are terminated using a null character.

    It doesn't make any sense to pad a string with null characters.


    Are you certain that you aren't supposed to just pad with space?

    Comment

    Working...