[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 :)
Re: convert a IP range string to Base64String for XML storage
vb.net Code:
Dim addresses = Apf.IPRange.Split("-"c)
Dim startAddress = IPAddress.Parse(addresses(0))
Dim endAddress = IPAddress.Parse(addresses(1))
Dim start64 = Convert.ToBase64String(startAddress.GetAddressBytes())
Dim end64 = Convert.ToBase64String(endAddress.GetAddressBytes())
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!
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:
Dim base64Strings = Array.ConvertAll(Apf.IPRange.Split("-"c), Function(s) IPAddress.Parse(s).GetAddressBytes())
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! :)
Re: [RESOLVED] convert a IP range string to Base64String for XML storage
Quote:
Originally Posted by
Lectere
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.
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!
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! :D :thumb: :check: