Pages

Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts

Wednesday, October 28, 2015

Obtinerea adresei IP in formatul standard dintr-un numar intreg

Calcularea adresei IP în formatul IPv4 dintr-un număr întreg pe o platforma little endian.


long IP = 3232679515;

byte[] bytes = new byte[4];


bytes[0] = (byte)((IP >> 0) & 0xFF);
bytes[1] = (byte)((IP >> 8) & 0xFF);
bytes[2] = (byte)((IP >> 16) & 0xFF);
bytes[3] = (byte)((IP >> 24) & 0xFF);

// all of the protocol layers in the TCP/IP suite are defined
// to be big endian
string ipText = bytes.Select(b => b.ToString())
 .Aggregate((ip, b) => (ip.Length == 0) ? b : ip + "." + b);

Console.WriteLine(ipText); // prints 91.198.174.192


Bineînțeles că nu trebuie să ne chinuim atât de mult să obținem o adresă IP dintr-un long. În .net există clasa IPAddress care reprezintă o adresa IP și furnizează diferite proprietăți și metode utile:


System.Net.IPAddress ip = new System.Net.IPAddress(3232679515);
Console.WriteLine(ip.ToString()); // prints 91.198.174.192


Am citit despre:
Big-Endian vs. Little Endian: Endianness Explained, http://www.barrgroup.com/Embedded-Systems/How-To/Big-Endian-Little-Endian


Referințe:
MSDN, https://msdn.microsoft.com/en-us/library/system.net.ipaddress(v=vs.110).aspx
Understanding IP Addresses, Subnets, and CIDR Notation for Networking, https://www.digitalocean.com/community/tutorials/understanding-ip-addresses-subnets-and-cidr-notation-for-networking

Thursday, September 03, 2015

Javacript, python si c# si operatia de impartire a numerelor intregi

În Python 2.7 găsesc supărător faptul ca împărțirea a doua numere întregi are ca rezultat tot un număr întreg chiar dacă împărțirea celor două numere nu este exactă. De la un limbaj dinamic m-aș fi așteptat să obțin un număr real, precum în Javascript:


C:\Users\>Daniel>node
> var a = 10;
undefined
> var b=3;
undefined
> a/b
3.3333333333333335

Javascript, intern, are doar singur tip de reprezentare a numerelor, 64-bit virgula mobila, acelasi cu double din c#.

Python în schimb suportă și tipul integer. Rezultatul împărțirii a doua numere întregi în Python 2.7 este întotdeauna un număr întreg:

In [1]: a = 10
In [2]: b = 3
In [3]: a/b
Out[3]: 3

Pentru a pentru a obține rezultatul corect trebuie sa convertim unul din operatori la tipul float.

In [5]: a/float(b)
Out[5]: 3.3333333333333335

Python 3 nu are această problemă, rezultatul împărțirii în astfel de cazuri fiind un număr real (true division).

Putem folosi și în Python2.7 operația de diviziune din Python 3  cu un import din __future__:

In [1]: from __future__ import division
In [2]: a = 10
In [3]: b = 3
In [4]: a/b
Out[4]: 3.33333333333333355


În C#, rezultatul împărțirii a doua numere întregi este tot un număr întreg, rezultat care nu ridică întrebări având în vedere faptul ca limbajul este static.


1
2
3
4
int a = 10;
int b = 3;

Console.WriteLine(a/b); // prints 3

Result: 3


REFERENCES:

- Python 2.7 Documentation, https://docs.python.org/2.7/library/stdtypes.html#numeric-types-int-float-long-complex
- Python 3.4 Documentation, https://docs.python.org/3.4/library/stdtypes.html#numeric-types-int-float-complex
- MDN, Numbers, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Numbers_and_dates
- How can I force division to be floating point in Python? http://stackoverflow.com/questions/1267869/how-can-i-force-division-to-be-floating-point-in-python
- Learning Python de Mark Lutz. Capitolul 5, Numeric Types
- Javascript: the Good Parts de Douglas Crockford. Capitolul 2. Grammar
- C# 5.0 in a Nutshell de Joe Albahari, capitolul 2 Numeric Types


Friday, June 26, 2009

An easy way to check if a number is odd or even

Using the bitwise operator AND you can check if a number is odd or even.

private bool IsOdd(int i)
{
return (i&1==1);
}


If the function IsOdd() returns true then the number is odd otherwise, the number is even.

I posted this entry in my new blog at http://www.elgrillo.eu/blog. The new blog is about programming.