Chr(0) in .NET causes string to return blank

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Chad Dalton (cdalton_at_jackhenry.com)
Date: 02/24/04


Date: Tue, 24 Feb 2004 06:11:42 -0800


The code worked perfectly fine in VB6 for converting a decimal length to
binary hex, but in .NET it only works when the iNum variable doesn't
contain a zero. If zero is passed in then I lose everything in the
string. Does anyone
know how to make the Chr(0) Function work exactly as it did in VB 6. If
you pass 342,2 as the arguments then everything works fine, but if you
pass 54,2 then it returns blank. Any help would be greatly
appreciated!!!!

Public Function SetBinaryLengthofMsg(ByRef iLen As Integer, ByRef
iPosLen As Integer) As String

iLen = decimal length value to be converted to binary
iPosLen = number of bytes in which to store binary length value (max
bytes = 3)

        Dim iNum As Integer
        Dim iRemainder As Short
        Dim iLoop As Short
        Dim lTmpLenVal As Integer
        Dim szTmpLenString As String
        Dim sb As New StringBuilder

        '---set temporary decreasing value to length value
        lTmpLenVal = lLen
        szTmpLenString = ""

        '---loop backwards breaking out length value into bytes
        For iLoop = lPosLen To 1 Step -1

            Select Case iLoop
                Case 3
                    iNum = Int(lTmpLenVal / 65536)
                    sb.Append(Chr(iNum))
                    lTmpLenVal = lTmpLenVal Mod 65536
                Case 2
                    iNum = Int(lTmpLenVal / 256)
                    sb.Append(Chr(iNum))
                    lTmpLenVal = lTmpLenVal Mod 256
                Case 1
                    iNum = lTmpLenVal
                    sb.Append(Chr(iNum))
            End Select

        Next iLoop

        '---return length value in binary hex
        SetBinaryLengthofMsg = sb.ToString()

    End Function

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Relevant Pages

  • Re: Unicode String Parameters in DLLs
    ... > NOT Unicode, so you might have heard some stuff about that. ... > One thing to watch out for is if your DLL routine puts a zero byte at the ... > string ); it does not terminate the string. ... >> We are converting our application to work with Unicode. ...
    (microsoft.public.word.vba.general)
  • Code worked fine in VB6 but not .NET
    ... The code worked perfectly fine in VB6 for converting a decimal length to ... binary hex, but in .NET it only works when the iNum variable doesn't ... If zero is passed in then I lose everything in the ... lPosLen As Integer) As String ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: how can one variable equal 2 things??
    ... be a boolean true since the variable exists and is not NULL, but testing if == 1 returns FALSE. ... If you compare a string to a number, an attempt is made to convert that string to a number first. ... If the string starts with valid number data, then that will be the used value, otherwise the comparison will be against zero. ... as well as the section on converting ...
    (comp.lang.php)
  • Re: Chr(0) in .NET causes string to return blank
    ... > binary hex, but in .NET it only works when the iNum variable doesn't ... If zero is passed in then I lose everything in the ... My guess is that you're actually creating the string properly, ... Character data *isn't* ...
    (microsoft.public.dotnet.framework)
  • Re: Code worked fine in VB6 but not .NET
    ... > The code worked perfectly fine in VB6 for converting a decimal length to ... If zero is passed in then I lose everything in the ... > Public Function SetBinaryLengthofMsg(ByRef lLen As Integer, ... > lPosLen As Integer) As String ...
    (microsoft.public.dotnet.languages.csharp)