2012-07-20 42 views
1

我想知道如何在x86機器上手動將IP地址轉換爲十六進制值。例如,這本書我讀給出192.168.42.72十六進制表示爲:將IP地址轉換爲十六進制

0x482aa8c0 

,但從來沒有解釋如何轉換工作。那麼,它是如何呢?

回答

7

當您將IP爲長整型,您需要按相反的順序每個字節和256^n乘以其中ñ是這個IP你」的八位位組的從零開始的反向索引

所以重做

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

看起來這本書正在倒退,但你明白了。

+0

另外值得注意的是,有函數來爲你做一個家庭,[INET_ATON](http://linux.die.net/man/3/inet_aton)et al – 2012-07-20 15:10:39

+1

它不一定會倒退,這可能是由於[endianness](http://en.wikipedia.org/wiki/Endianness),它出現在另一個方向 – 2012-07-20 15:15:10

+0

@DanielDiPaolo不是整個TCP堆棧指定爲big-endian?對不起,我知道這是一個陳舊的問題。 – apraetor 2016-03-07 18:02:08

1

有時你會看到它的格式是這樣的HEX IP地址。

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 
0

第一轉換192.168.42.72成二進制數原樣 11000000.10101000.00101010.01001000 再取4-4位爲拍攝二進制到十六進制數字轉換.. 所以.. 1100 0000. 1010 1000. 0010 1010. 0100 1000 並且此Ip的十六進制爲:C 0. A 8.2 A.4 8 現在以精確的十六進制表示IP地址。 十六進制代碼是:0xC0A82A48。

,我知道最簡單的方法...

1

看不到任何PowerShell的答案,所以這裏去。

該第一個示例將IP地址轉換爲十六進制。

$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 

結果

0xC0A82A48

這一個十六進制轉換回十進制IP地址。

$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 

結果

192.168.42.72

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 
相關問題