CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2003
    Location
    AR
    Posts
    228

    URGENT help converting IP from Long to String

    I have a list of IP numbers in the Long format, and this function is supposed to convert them to the string format (x.x.x.x)

    However the function only converts up to certain value, so is not entirely working. Any help will be really appreciated, this is an urgent project.

    Code:
    ' Inputs: IP Address as a Long Number (no dots)
    ' Returns: Returns the string representation of an IP Address ("192.168.0.1")
    
        Function CStrIP(ByVal anNewIP As Long) As String
            Dim lsResults        ' Results To be returned
            Dim lnTemp            ' Temporary value being parsed
            Dim lnIndex            ' Position of number being parsed
            ' If pulling number from an Access Database,
            ' The variable Type "Long" ranges from -2147483648 To 2147483647
            ' You will first need To add 2147483648 to the number to parse correctly.
            ' anNewIP = anNewIP + 2147483648
            ' Parse highest numbers first
            For lnIndex = 3 To 0 Step -1
            
                ' Parse the current value For this position
                lnTemp = Int(anNewIP / (256 ^ lnIndex))
                
                ' Append the number To the final results delimited by a dot
                lsResults = lsResults & lnTemp & "."
                
                ' Remove the number that we just parsed
                anNewIP = anNewIP - (lnTemp * (256 ^ lnIndex))
                
            Next
            
            ' Cut off last dot
            lsResults = Left(lsResults, Len(lsResults) - 1)
            
            ' Return the results
            CStrIP = lsResults
            
        End Function

  2. #2
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: URGENT help converting IP from Long to String

    You may try my code, it's a lot simplier
    Code:
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
    
    Private Function IPString(ByVal Value As Long) As String
    Dim b(3) As Byte
      
      CopyMemory b(0), Value, 4&
      IPString = b(3) & "." & b(2) & "." & b(1) & "." & b(0)
      
      Erase b
      
    End Function
    Busy

  3. #3
    Join Date
    Feb 2003
    Location
    AR
    Posts
    228

    Thumbs up Re: URGENT help converting IP from Long to String

    Thanks, but I keep having the same problem. I noticed my IP numbers are actually of type Double, not Long.... any ideas how to deal with this?
    Last edited by Andrex; August 24th, 2006 at 10:37 AM.

  4. #4
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: URGENT help converting IP from Long to String

    Convert them to long before you do the above trick by Thread1.

    dim ipl as long
    ipl = CLng(IPdouble)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured