Please help with ipaddress !!

smallzoo

Member
Joined
Apr 29, 2008
Messages
8
Programming Experience
3-5
I am reading a 32 bit register (UINT32) and I need a simple function which will convert it into an ipaddress

I have been told by the C++ programmer the following

The IP Address is encoded as: www.xxx.yyy.zzz e.g. 192.168.001.001

32 bit number (Uint_32) 0xaabbccdd named IPAddr

www = (IPAddr >> 24) & 0xFF
xxx = (IPAddr >> 16) & 0xFF
yyy = (IPAddr >> 8) & 0xFF
zzz = (IPAddr & 0xFF


At the moment I read the register into a byte array g3_data(0) , g3_data(1) , g3_data(2) , g3_data(3)
and then convert it to a string for displaying ( the same routine is used for other registers )

valstr = BitConverter.ToInt32(g3_data, 0).ToString()


Can someone please show me how to convert this byte array or string into the ip address in vb.net

Many thanks
 
If there's nothing fancy being done with the values in your 32bit integer, then all you need to do is shift the bits of each member of your byte array to the right by the number of bits necessary to make it an 8 bit number....which is easy to do by dividing the value by two to the power of the number of bits.

e.g if your byte array member equals 11111111000000000000000000000000 then you're going to need to divide it by (2 ^ 24) which will make it 00000000000000000000000011111111 = 255

... so for each member (except the last, since it's already in the 8bit range) you'd use (2^24), (2^16) and (2^8)

That should give you the values for each octect of the IP address.

--edit--
My bad - creating the byte array will already have split the 32 bit integer into individual bytes, so the above isn't necessary in this instance.
 
Last edited:
Sorry I am still struggling...

Can someone show me..I am still stuck..

Basics of my code

Dim g3_data(4) As Byte
.....
Value I am getting
g3_data(0) ---> 3B
g3_data(1) ---> 00
g3_data(2) ---> A8
g3_data(3) ---> C0

The actual IP address is 192.168.000.059

I just cant seem to get ths g3_data byte array into the ip address

THANKS !!
 
Google 'hex to decimal conversion' - you'll be surprised (or not) by what you get.

Here's a hint.... 3B is 59 in decimal.
 
Back
Top