Question Char to String conversion

danfloun

Member
Joined
Apr 5, 2010
Messages
23
Programming Experience
Beginner
Hey,

I've got a basic app that reads an ip from a webpage (whatismyip.org) using streamreader and I use the following code to make sure I read just the first 15 characters from the stream.

VB.NET:
Dim ip(15) As Char
                Do While sr.Peek >= 0
                    sr.Read(ip, 0, ip.Length)
                Loop

This all works perfectly.
The problem is I need to verify that an ip has been retrieved and not some random crap, which would happen if the webpage changed for example.

I've tried using the IPAddress.TryParse() command to verify the address, but it does work and returns false because an input string is required and not a char.

So I've tried;
VB.NET:
Dim isValidIP As String = CStr(ip)

VB.NET:
 Dim isValidIP As New String(ip)

VB.NET:
 Dim isValidIP As String = ip.string

All of which, when validated with IPAddress.TryParse() return false.
Yet if I try this;

VB.NET:
 Dim ip As String = "87.34.67.112"
Dim isValidIP as Boolean
isValidIP = IPAddress.TryParse(ip, Nothing)

The code returns true as it should.
So it's definitely at problem with the char to string conversion.... maybe encoding?

Thanks
Danny
 
Use ReadLine method of StreamReader instead. Call it only once without looping, only the first line is relevant here anyway. Then you can use the IPAddress class for parsing/validating the input.
VB.NET:
Dim input As String = reader.ReadLine
reader.Close()
Dim ip As Net.IPAddress = Nothing
If Net.IPAddress.TryParse(input, ip) Then
    'this is a valid IP address
End If
 
Use ReadLine method of StreamReader instead. Call it only once without looping, only the first line is relevant here anyway. Then you can use the IPAddress class for parsing/validating the input.
VB.NET:
Dim input As String = reader.ReadLine
reader.Close()
Dim ip As Net.IPAddress = Nothing
If Net.IPAddress.TryParse(input, ip) Then
    'this is a valid IP address
End If

JohnH, thanks.

I thought I might end up changing Read() to something else.

Just something I'm not certain of, I read the MSDN docs but I'm still not clear.
What does the second parameter in the TryParse do?
In your example that would be 'ip'.

I realise you define ip as an ip address, but I don't see what it's for, is it a reference of some kind?

Cheers
Danny
 
TryParse methods in general take some string input and tries to parse it to the designated type, without throwing an exception if the conversion fails. Most data types that can be converted from a string has one. The result of the conversion is what is returned in the second ByRef parameter. You can click the "IPAddress Methods" from the main help topic I linked to then click 'TryParse method' to get detailed explainations for that method, or any class member for that matter.
 
TryParse methods in general take some string input and tries to parse it to the designated type, without throwing an exception if the conversion fails. Most data types that can be converted from a string has one. The result of the conversion is what is returned in the second ByRef parameter. You can click the "IPAddress Methods" from the main help topic I linked to then click 'TryParse method' to get detailed explainations for that method, or any class member for that matter.

Thanks again, I understand now.

However, regarding my original problem, I still find myself wanting to use the original idea using read(), even though it's more code and at the moment, not working.

Your method worked fine and the tryparse recognised the ip string.

But is still feel it's not great in that, if I change the uri for the string retrieval to say google.co.uk, the readline() method streams a lot of html head data, I mean lots, even though it only requests a line.

I don't like this idea that I'm downloading far more than I need to if one of the websites change. By specifying 15 characters using read(), I always get just that, regardless of page and it's also gives a slightly faster response.

I can't understand why, when I convert the char to string, that when outputting the file to the UI as a string it shows the IP fine, however performing a tryparse on it fails.

Any ideas?

Cheers
Danny
 
I now understand why it would not validate the IP address.
When I specify a buffer size, if the ip address is anything less than 15 characters, which can occur obviously, then there must be some spaces at the end of the char array.

If I change the buffer to the exact size of my present wan IP address i.e. ip(11) then all works and the ip gets validated, but any buffer longer than the actual ip and ip validation fails.

So my next problem is how to remove empty cells within the char array!?
 
Read function returns the number of bytes actually read of what was requested. This tells you how many bytes in array is filled. Then you can use this number when constructing the String from the buffer.
VB.NET:
Dim buffer(14) As Char
Dim count As Integer = reader.Read(buffer, 0, buffer.Length)
reader.Close()
Dim s As New String(buffer, 0, count)
 
Read function returns the number of bytes actually read of what was requested. This tells you how many bytes in array is filled. Then you can use this number when constructing the String from the buffer.
VB.NET:
Dim buffer(14) As Char
Dim count As Integer = reader.Read(buffer, 0, buffer.Length)
reader.Close()
Dim s As New String(buffer, 0, count)

That's great John Cheers, I'll try that when I get back tonight.
Quite interesting, all the different ways there are to get to the same result.

I got so much to learn! :|
 
That's great John Cheers, I'll try that when I get back tonight.
Quite interesting, all the different ways there are to get to the same result.

I got so much to learn! :|

Dear John,

That worked perfectly, I tried changing the buffer size and it always validated correctly. I new there was a simple way to do it, I never knew how.
Now I do and I understand thankfully.

Thanks again.
 
Back
Top