将IP地址转换为十六进制 [英] Converting IP Address to Hex
问题描述
我想知道如何在x86机器上手动将IP地址转换为十六进制值。例如,我读的这本书给出了192.168.42.72的十六进制表示形式:
0x482aa8c0
但从未解释转换是如何工作的。那么,它如何呢?
当您将IP转换为长整数时,需要按相反顺序取每个八位字节它由 256 ^ n
其中 n 是八位字节的从零开始的反向索引
因此对于这个ip,你正在做
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $(72 * 256 ^ 0)+(42 * 256 ^ 1)+ (168 * 256 ^ 2)+(192 * 256 ^ 3)
= 3232246344
= 0xc0a82a48
看起来这本书正在倒退,但你明白了。
I was wondering how to manually convert an IP address to a hex value on an x86 machine. For example, the book I was reading gives the hex representation of 192.168.42.72 as:
0x482aa8c0
but never explains how the conversion works. So, how does it?
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.
这篇关于将IP地址转换为十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!