How do I manually convert a hex value to a IP address without a online converter?

For example what is 80D00297 in normal IP dot notation?

  • 3
    windows cmd> ping 0x80D00297 => Pinging with 32 bytes of data: – guest-vm Jul 23 '16 at 7:07
  • i think you MIGHT be confusing a mac address and IP address. Where are you seeing a IP address represented as a hex value? – djsmiley2kStaysInside Jul 23 '16 at 8:18
  • 1
    @djsmiley2k It's not a MAC address. MAC Addresses contain 6 groups of two hexadecimal digits not 4. – DavidPostill Jul 23 '16 at 8:34

Using sed and bash

$ printf '%d.%d.%d.%d\n' $(echo 80D00297 | sed 's/../0x& /g')

How it works:

  • sed is used to reformat the number:

    $ echo 80D00297 | sed 's/../0x& /g'
    0x80 0xD0 0x02 0x97 
  • The %d format of printf is used to convert the hex numbers to decimal.

Using GNU awk

$ echo 80D00297 | gawk --non-decimal-data '{for (i=1;i<=NF;i++) printf "%d%s","0x"$i,(i==NF?"\n":".")}' FPAT='..'

This uses FPAT='..' to divide the input into two character fields. We then loop through each field, put a 0x in front of it, and feed it to printf with the %d format so that the hex number is converted to decimal.

A seemingly complex part of the above is the ternary statement (i==NF?"\n":"."). This statement returns a newline character if we are on the last field, i==NF. For other values of i, it returns a period, .. In this way, the numbers are separated with a period but the final number is followed by a newline character.

  • 2
    Probably due to something related to endianness, but this reversed the order for me, so I had to modify it to sed 's/../0x&\n/g' | tac. – muru Jun 21 '18 at 0:12

Presuming you mean convert it to a string or dotted-notation representation

The first thing you need to know is whether the address is in network byte order, or host byte order. Network byte order is big-endian, whereas intel-based computers are little-endian. You can convert one to the other using a function called ntohl, which basically takes each byte (2 hex characters) from the address, and reverses them, so 80d00297 would become 9702d080

Then you take each byte, and convert to decimal. You can use a calculator for this.

80 => 128
d0 => 208
02 => 2
97 => 151

so, if that address is in host byte order, the address is

  • Correct answer! The question emphasized "manually". I would think a step by step calculation like this is what is required – papigee Sep 12 '19 at 14:33

You can use ctf-party (disclaimer: I'm the author)

require 'ctf_party'

'80D00297'.from_hexip # => ""
'80D00297'.from_hexip(nibble: :low) # => ""

On Unix some files such as /proc/net/tcp use low nibble first (little endian).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.