# Convert IP Address to Hex

## Recommended Posts

Hey Guys,

I would like to take a string that has a dotted decimal IP Address string and convert the address to its Hex equivalent. Of course each octet could have anywhere from 1 to 3 characters since each octet can contain numeric characters from 0-255.

I am just looking for some suggestions or known UDFs that might help me. If it doesn't exist, any ideas on what course to take would be helpful also. Thanks in advance

Justin

##### Share on other sites

There's possibly a more code-efficient way, but I'd just take the IP, use stringsplit to get each octet, then use hex to convert each decimal octet to it's hexadecimal value.

```\$ip = StringSplit("192.168.1.1", ".")
\$hexIP = hex(\$ip[1], 3) & "." & hex(\$ip[2], 3) & "." & hex(\$ip[3], 3) & "." & hex(\$ip[4], 3)```
Edited by Khab

##### Share on other sites

There's possibly a more code-efficient way, but I'd just take the IP, use stringsplit to get each octet, then use hex to convert each decimal octet to it's hexadecimal value.

```\$ip = StringSplit("192.168.1.1", ".")
\$hexIP = hex(\$ip[1], 3) & "." & hex(\$ip[2], 3) & "." & hex(\$ip[3], 3) & "." & hex(\$ip[4], 3)```

Hey Khab,

Thank you for the response and sorry about the delay. Your suggestion worked great, however I had to modify it a little bit to my needs. The most important change that I made was that the hex value for each octet is only 2 digits vs. the 3 that you specified in the hex() parameter. Here is how I modified it:

```\$ip = StringSplit("192.168.1.1", ".")
\$hexIP = hex(\$ip[1], 2) & hex(\$ip[2], 2) & hex(\$ip[3], 2) & hex(\$ip[4], 2)```

Also notice that I removed the "." in the \$hexIP string since I was going to use this in a raw packet through Winpcap and the raw data is only in hex.

Thanks again

Justin

## Create an account

Register a new account

×

• Wiki

• Back

• Git