1

我想知道如何在 x86 机器上手动将 IP 地址转换为十六进制值。例如,我正在阅读的书将 192.168.42.72 的十六进制表示为:

    0x482aa8c0

但从不解释转换是如何工作的。那么,它是怎么做到的呢?

4

5 回答 5

9

When you convert an IP to a long integer, you take each octet in reverse order and multiply it by 256^n where n is the zero-based reverse index of the octet

So for this ip you're doing

(72 * 256^0) + (42 * 256^1) + (168 * 256^2) + (192 * 256^3)
= 3232246344
= 0xc0a82a48

It looks like the book is doing it backwards, but you get the idea.

于 2012-07-20T15:09:29.883 回答
1

Don't see any powershell answers, so here goes.

This first sample converts IP address to hex.

$Octet1 = "{0:X2}" -f 192
$Octet2 = "{0:X2}" -f 168
$Octet3 = "{0:X2}" -f 42
$Octet4 = "{0:X2}" -f 72
$IPAddress = "0x"+$Octet1 + $Octet2 + $Octet3 + $Octet4
$IPAddress

Result

0xC0A82A48

and this one converts hex back to decimal IP address.

$Octet1 = "{0:D}" -f 0xC0
$Octet2 = "{0:D}" -f 0xA8
$Octet3 = "{0:D}" -f 0x2A
$Octet4 = "{0:D}" -f 0x48
$IPAddress = $Octet1 +"."+ $Octet2 +"."+ $Octet3 +"."+ $Octet4
$IPAddress

Result

192.168.42.72

于 2014-01-26T23:12:07.113 回答
1

有时您会看到带有 IP 地址的 HEX 格式如下。

 0xC0.0xA8.0x2A.0x48

这是我在脑海中的做法,因为我不擅长处理大数字,因为十六进制是基于 16 的。下面的图表是左边的 DEC 和右边的 HEX。

0 = 0
1 = 1
2 = 2
3 = 3
4 = 4
5 = 5
6 = 6
7 = 7
8 = 8
9 = 9
10 = A
11 = B
12 = C
13 = D
14 = E
15 = F

然后一旦你记住了图表,这只是基本的数学

192 = C0 =  (192/16) = 12.0 =  take the remainder (0 x 16) = 0 convert it to Hex (0)      
then take the result (12) divide it by 16 (12/16) and if it's less then 1 then just 
covert the remainder to hex 12 = C then add it up backwards for C0

168 = A8 = (168/16) = 10.8 = he remainder (.8 x 16) = 12.8 convert it to hex (A) then 
take the result (12) divide it by 16 (12/16) and if it's less then 1 then just covert 
the remainder to hex 0 = 0 then add it up backwards for 0A8 or A8

42  = 2A = (42/16) = 2.625 = The remainder (.625 x 16) = 10 convert it to hex (A) then 
take the result (2) divide it by 16 (2/16) and if it's less then 1 then just covert the  
remainder to hex 2 = 2 then add it up backwards for 2A

72  = 48 = Your turn
于 2012-07-24T19:25:23.133 回答
0

首先将 192.168.42.72 转换为二进制数 as- 11000000.10101000.00101010.01001000 然后在二进制到十六进制数转换中取 4-4 位.. 所以.. 1100 0000. 1010 1000. 0010 1010. 0100 1000 和十六进制为C 0. A 8.2 A.4 8 现在以精确的十六进制表示 IP 地址。十六进制代码为:0xC0A82A48。

我知道的最简单的方法...

于 2013-02-09T15:50:53.867 回答
0
$ip = "192.168.2.14"
$ar = $ip.Split('.')
$Octet1 = "{0:X2}" -f [int]$ar[0]
$Octet2 = "{0:X2}" -f [int]$ar[1]
$Octet3 = "{0:X2}" -f [int]$ar[2]
$Octet4 = "{0:X2}" -f [int]$ar[3]
$IPAddress = $Octet4 + $Octet3 + $Octet2 + $Octet1
$IPAddress
于 2015-02-27T23:26:31.223 回答