X86 将IP地址转换为十六进制

X86 将IP地址转换为十六进制,x86,hex,X86,Hex,我想知道如何在x86机器上手动将IP地址转换为十六进制值。例如,我正在读的书给出了192.168.42.72的十六进制表示,如下所示: 0x482aa8c0 但从未解释转换是如何工作的。那么,它是如何实现的呢?当你把一个IP转换成一个长整数时,你以相反的顺序取每个八位字节,然后将其乘以256^n,其中n是八位字节从零开始的反向索引 所以对于这个ip,你正在做什么 (72 * 256^0) + (42 * 256^1) + (168 * 256^2) + (192 * 256^3) =

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

    0x482aa8c0

但从未解释转换是如何工作的。那么,它是如何实现的呢?

当你把一个IP转换成一个长整数时,你以相反的顺序取每个八位字节,然后将其乘以
256^n
,其中n是八位字节从零开始的反向索引

所以对于这个ip,你正在做什么

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

看起来这本书是在倒转,但你明白了。当你把一个IP转换成一个长整数时,你把每一个八位字节按相反的顺序,乘以256^n,其中n是八位字节从零开始的反向索引

所以对于这个ip,你正在做什么

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

这本书看起来像是在倒着写,但你明白了。有时候你会看到它的格式是这样的,十六进制的IP地址

 0xC0.0xA8.0x2A.0x48
我是这样想的,因为我不擅长大数,因为十六进制是以16为基础的。下图左边是DEC,右边是十六进制

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

有时你会看到它的格式是这样的十六进制与IP地址

 0xC0.0xA8.0x2A.0x48
我是这样想的,因为我不擅长大数,因为十六进制是以16为基础的。下图左边是DEC,右边是十六进制

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

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


我知道的最简单的方法…

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


我所知道的最简单的方法…

看不到任何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

看不到任何powershell的答案,所以现在开始

$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
第一个示例将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

另外值得注意的是,有一系列函数可以为您执行此操作,et alIt不一定是向后执行的,可能是因为它是从另一个way@DanielDiPaolo整个TCP堆栈不是都是big-endian吗?抱歉,我知道这是一个陈腐的问题。另外值得注意的是,有一系列函数可以为您执行,等等,它不一定是向后执行的,可能是因为它是另一个way@DanielDiPaolo整个TCP堆栈不是都是big-endian吗?对不起,我知道这是个陈腐的问题。
$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