NetworkToHostOrder ~ HostToNetworkOrder - algorithm in C#

  • Thread starter Thread starter Martin Greul
  • Start date Start date
M

Martin Greul

Hello together,

int lengthInNetworkOrder =
IPAddress.NetworkToHostOrder(data.Length + 4);

I want to make this function by hand.
How it is possible?
Does somebody know the algorithm?

And also for


int lengthInNetworkOrder =
IPAddress.HostToNetworkOrder(512);

Have a nice weekend.

Greeting Martin
 
Martin said:
int lengthInNetworkOrder =
IPAddress.NetworkToHostOrder(data.Length + 4);

I want to make this function by hand.
How it is possible?
Does somebody know the algorithm?

And also for


int lengthInNetworkOrder =
IPAddress.HostToNetworkOrder(512);

The functions are actually identical.

The just swap byte 0 and 3 and byte 1 and 2.

Some old code from the shelf:

class EndianUtil
{
public static short Swap(short v) {
return (short)(((v >> 8) & 0x00FF) | ((v << 8) & 0xFF00));
}
public static int Swap(int v) {
uint v2 = (uint)v;
return (int)(((v2 >> 24) & 0x000000FF) |
((v2 >> 8) & 0x0000FF00) |
((v2 << 8) & 0x00FF0000) |
((v2 << 24) & 0xFF000000));
}
}

Arne
 
Hello Arne,
The functions are actually identical.

The just swap byte 0 and 3 and byte 1 and 2.

Some old code from the shelf:

class EndianUtil
{
public static short Swap(short v) {
return (short)(((v >> 8) & 0x00FF) | ((v << 8) & 0xFF00));
}
public static int Swap(int v) {
uint v2 = (uint)v;
return (int)(((v2 >> 24) & 0x000000FF) |
((v2 >> 8) & 0x0000FF00) |
((v2 << 8) & 0x00FF0000) |
((v2 << 24) & 0xFF000000));
}
}
fine.

How copy you the new value in a array?

Like this.
Byte[] bytesSent = new Byte[1024];

A)
in the first four Bytes.
B)
in Byte 20 to 23?

Thanks.


Greeting
Martin
 
Martin said:
Hello Arne,
The functions are actually identical.

The just swap byte 0 and 3 and byte 1 and 2.

Some old code from the shelf:

class EndianUtil
{
public static short Swap(short v) {
return (short)(((v >> 8) & 0x00FF) | ((v << 8) & 0xFF00));
}
public static int Swap(int v) {
uint v2 = (uint)v;
return (int)(((v2 >> 24) & 0x000000FF) |
((v2 >> 8) & 0x0000FF00) |
((v2 << 8) & 0x00FF0000) |
((v2 << 24) & 0xFF000000));
}
}
fine.

How copy you the new value in a array?

Like this.
Byte[] bytesSent = new Byte[1024];

A)
in the first four Bytes.
B)
in Byte 20 to 23?

I would probably just use a for loop.

Simple and easy.

Arne
 
Back
Top