VS 2008 [RESOLVED] convert a IP range string to Base64String for XML storage-VBForums
Results 1 to 8 of 8

Thread: [RESOLVED] convert a IP range string to Base64String for XML storage

  1. #1

    Thread Starter
    Addicted Member Lectere's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    224

    Resolved [RESOLVED] convert a IP range string to Base64String for XML storage

    Hello dear friends at the VB forums!

    Been a while since I last posted here. I'm busy on a new project and I need to convert a string which represents a IP range (like this: 10.0.0.0-10.0.0.254), to two Base64 strings which represents the first and second IP adress in this range definition. Like this: (start IP: CgAAAA== End IP: CgAA/g==)

    I've got it to work, but it such an ugly code, and I know it can be done with 2, or at max 4 lines code. But I'm not so handy with the Base64 string conversion jet. So this is where I ask for help from you guys!

    Here's what I got (in goes: Apf.Iprange = "10.0.0.0-10.0.0.254")

    Code:
                        Dim StartIp As String = Apf.IPRange.Split("-")(0)
                        Dim EndIp As String = Apf.IPRange.Split("-")(1)
                        Dim StartNumbers() As String = StartIp.Split(".")
                        Dim EndNumbers() As String = EndIp.Split(".")
                        Dim StartBytes As String
                        Dim EndBytes As String
                        Dim sBytes(3) As Byte
                        Dim eBytes(3) As Byte
    
                        sBytes(0) = Val(StartNumbers(0))
                        sBytes(1) = Val(StartNumbers(1))
                        sBytes(2) = Val(StartNumbers(2))
                        sBytes(3) = Val(StartNumbers(3))
    
                        eBytes(0) = Val(EndNumbers(0))
                        eBytes(1) = Val(EndNumbers(1))
                        eBytes(2) = Val(EndNumbers(2))
                        eBytes(3) = Val(EndNumbers(3))
    
                        StartBytes = Convert.ToBase64String(sBytes)
                        EndBytes = Convert.ToBase64String(eBytes)
    Thanks for thinking along! So credits for the person who gets this done with the least amount of operators

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,099

    Re: convert a IP range string to Base64String for XML storage

    vb.net Code:
    1. Dim addresses = Apf.IPRange.Split("-"c)
    2. Dim startAddress = IPAddress.Parse(addresses(0))
    3. Dim endAddress = IPAddress.Parse(addresses(1))
    4. Dim start64 = Convert.ToBase64String(startAddress.GetAddressBytes())
    5. Dim end64 = Convert.ToBase64String(endAddress.GetAddressBytes())
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member Lectere's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    224

    Re: convert a IP range string to Base64String for XML storage

    Thanks again, Jmcilhinney!

    Code:
                        Dim startAddress = IPAddress.Parse(Apf.IPRange.Split("-"c)(0))
                        Dim endAddress = IPAddress.Parse(Apf.IPRange.Split("-"c)(1))
                        Dim start64 = Convert.ToBase64String(startAddress.GetAddressBytes())
                        Dim end64 = Convert.ToBase64String(endAddress.GetAddressBytes())
    4 lines sweet!, the 'c' after the .split('-"c) typo?, or does that mean to exclude the "-" character?

    Thanks again m8!

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,099

    Re: [RESOLVED] convert a IP range string to Base64String for XML storage

    It's not a typo. It's the way the code should be. It specifies that what's between the double quotes is a Char rather than a String. That overload of String.Split requires a Char, so a String must be implicitly converted, which won't happen if Option Strict is On, which it should be.

    This is very bad practice:
    Code:
    Dim startAddress = IPAddress.Parse(Apf.IPRange.Split("-"c)(0))
    Dim endAddress = IPAddress.Parse(Apf.IPRange.Split("-"c)(1))
    You are splitting the same String in exactly the same way twice. You should do as I did and split it once and assign the result to a variable, then use that variable multiple times. Alternatively, you could use a With block.

    By the way, if you don't mind an array rather than two discrete variables then you can do it in one line:
    vb.net Code:
    1. Dim base64Strings = Array.ConvertAll(Apf.IPRange.Split("-"c), Function(s) IPAddress.Parse(s).GetAddressBytes())
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member Lectere's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    224

    Re: [RESOLVED] convert a IP range string to Base64String for XML storage

    This is what I mean!

    Code:
    Dim base64Strings = Array.ConvertAll(Apf.IPRange.Split("-"c), Function(s) IPAddress.Parse(s).GetAddressBytes())
    f-ing brillant!

    But I have to use it as a string, not a byte array. But this is a very interesting command, array.convertall

    Thanks for opening my eyes again!

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,099

    Re: [RESOLVED] convert a IP range string to Base64String for XML storage

    Quote Originally Posted by Lectere View Post
    This is what I mean!

    Code:
    Dim base64Strings = Array.ConvertAll(Apf.IPRange.Split("-"c), Function(s) IPAddress.Parse(s).GetAddressBytes())
    f-ing brillant!

    But I have to use it as a string, not a byte array. But this is a very interesting command, array.convertall

    Thanks for opening my eyes again!
    It's not a Byte array. It's a String array. That's why I named it 'base64Strings'. Just hover over it in the IDE and Intellisense will tell you the type. You would get the two base64 strings from its two elements.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,099

    Re: [RESOLVED] convert a IP range string to Base64String for XML storage

    Oops! My mistake. I left a little bit out:
    Code:
    Dim base64Strings = Array.ConvertAll(Apf.IPRange.Split("-"c), Function(s) Convert.ToBase64String(IPAddress.Parse(s).GetAddressBytes()))
    NOW it's a String array. That's what I get for typing straight into the forum rather than into an IDE!
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Addicted Member Lectere's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    224

    Re: [RESOLVED] convert a IP range string to Base64String for XML storage

    If you ask me, you did that on purpose, just to get your number of post on level

    hehehe, no thanks a lot m8! really an eye opener!

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.