How to make the correct Ipaddress.Parse conversion

Asked

Viewed 28 times

0

Hello, I’m making a communication with an equipment using socket. I’m using the library System.Net to convert the ip and pass the port as parameter in the IPEndPoint to use in the SocketEquipamento.Connect

I’m doing it this way:

IPAddress ipAddress = IPAddress.Parse("192.168.000.077");
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
SocketEquipamento.Connect(remoteEP);
Console.WriteLine("Conectado com sucesso"); 

Thus 192.168.000.077 the IPAddress.Parse is converting to the ipAddress the following result 192.168.0.63

If you pass the ip 192.168.0.77 it keeps the ip and makes the connection.

I wonder why he’s doing this when he passes 192.168.000.077.

Is there another way to make this conversion? Or should I have a treatment when receiving the ip taking the 0 lefty?

1 answer

0


A number with zeros on the left is an octal number, that is, a number written on base 8.
Thus,

(octal) 077 = (decimal) (0 x 64) + (7 x 8) + (7 x 1) = 63

In other words, generally avoid writing numbers with zeros on the left.

  • Has some function already System.Net who does this check? If there is no example of treatment in this case?

  • I don’t understand your question... nay use zeros on the left: IPAddress ipAddress = IPAddress.Parse("192.168.0.77");

  • Yes I understood I must use 192.168.0.77 , however I need to remove these zero why the ip and dynamic I spent fixed on the question just to be more clear what is happening.

  • if you are reading these numbers from a configuration file, then generally you too nay should use numbers with zeros on the left in the configuration file

  • This coming from a database, I need a function that takes these zeros, do you know any? I thought to use the Replace but will remove all zeroes.

  • I don’t work with C#, but I imagine you can test if the field is all filled with zeros, in this case you replace with only one zero...in the other case I imagine that Replace should be enough...otherwise I can’t help but have no experience with C#

  • Thanks, I managed to solve creating a method that does this.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.