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?
33 Answers
Using sed and bash
$ printf '%d.%d.%d.%d\n' $(echo 80D00297 | sed 's/../0x& /g')
128.208.2.151How it works:
sedis used to reformat the number:$ echo 80D00297 | sed 's/../0x& /g' 0x80 0xD0 0x02 0x97The
%dformat ofprintfis 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='..'
128.208.2.151This 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.
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 => 151so, if that address is in host byte order, the address is 128.208.2.151
1You can use ctf-party (disclaimer: I'm the author)
require 'ctf_party'
'80D00297'.from_hexip # => "128.208.2.151"
'80D00297'.from_hexip(nibble: :low) # => "151.2.208.128"On Unix some files such as /proc/net/tcp use low nibble first (little endian).