将IP地址转换为十六进制 [英] Converting IP Address to Hex

查看:409
本文介绍了将IP地址转换为十六进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆