IPv6 地址的算术(大整数)
我正在使用以下形式的 IPv6 地址:
FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
在内部,我将它们存储在一个数组中:
TIp6Bytes = array [0..15] of Byte;
我需要以多种方式操作 IPv6 地址,包括添加、除法、乘法等。任何人都可以提出一个好方法来做到这一点吗?
我想我应该提到我正在使用 Delphi 2009
I'm working with IPv6 addresses in the form:
FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
Internally, I store them in an array:
TIp6Bytes = array [0..15] of Byte;
I need to manipulate the IPv6 addresses in a number of ways including adding, dividing, multiplying etc. Can anyone suggest a good way to do this?
I guess I should have mentioned that I'm working with Delphi 2009
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Jes Klinke 在此处为 Pascal 编写了一个 bignum 单元。
免责声明:我个人没有使用过这个库。
Jes Klinke wrote a bignum unit for Pascal here.
Disclaimer : I have not used this library personally.
在尝试了许多建议之后,我找不到一个能够满足我所有需求并且没有错误的库。 我进行了一些搜索,发现了 Alex Ciobanu 的一个相对较新的库,它无缝地处理 BigIntegers(和 Big Cardinals),允许您以与操作普通整数、基数等相同的方式操作它们。
除了 BigIntegers 之外,该库还提供了许多非常有用的功能。 来自自述文件:
(列表、字典、哈希集等)。
在一些结构中(以某种方式
相当于.NET的DateTime
结构)
一组默认的“支持类”
每个内置的 Delphi 类型(用作
集合中的默认值)。 风俗
“类型支持”类可以是
注册您的自定义数据
类型。
该库正在积极开发中。 事实上,作者在一天内修复了我发现的一个小错误。
您可以在 Alex 的博客上了解有关该库的更多信息,并从 Google 代码。
After trying many of the suggestions I could not find a library that fulfilled all my needs and were bug free. I searched a little harder and found a relatively new library by Alex Ciobanu which does BigIntegers (and Big Cardinals) seamlessly allowing you to manipulate them in much the same way as you manipulate normal Integers, Cardinals etc.
As well as BigIntegers, the library also provides a number of very useful features. From the readme:
(List, Dictionary, HashSet, etc).
in a few structures (somehow
equivalent to .NET's DateTime
structure)
set of default "support classes" for
each built-in Delphi types (used as
defaults in collections). Custom
"type support" classes can be
registered for your custom data
types.
The library is being actively developed. In fact, the author fixed a small bug I found within a day.
You can read more about the library on Alex's blog and download DeHL from Google code.
我曾经编写过一个 IPv4 和 IPv6 转换单元,其中包括自定义变体类型对于两种类型的 IP 地址。
例如,使用这些 Variant 类型,可以进行以下示例算术和转换:
自定义变体类型确实非常强大。
I once wrote a IPv4 and IPv6 conversion unit including a custom variant type for both types of IP addresses.
For instance, with these Variant types, the following example arithmetics and conversions are possible:
Custom variant types really are quite powerfull.
我想说的是,如果你会加法,你就可以用它来用加法进行减法、乘法和除法。 我是否应该假设溢出将被简单地忽略?
我似乎记得使用 XOR 添加面向位的变量的方法。 我现在正在寻找这个答案。
希望这将为您指明正确的方向。 如果我能找到该 XOR 代码,我会将其发布给您。
这里是:
按位运算
异或常用于按位运算。 例子:
1 异或 1 = 0
1 异或 0 = 1
1110 xor 1001 = 0111 (这相当于不进位的加法)
参考是:
http://www.absoluteastronomy.com/topics/Exclusive_disjunction
I would say that if you can add, you can then use it to subtract, multiply and divide using addition. Should I assume overflows will be simply ignored?
I seem to recall a method of adding bit-oriented variables using XOR. I am looking for that answer now.
Hopefully, this will point you in the right direction. If I can find that XOR code, I will post it for you.
Here it is:
Bitwise operation
Exclusive disjunction is often used for bitwise operations. Examples:
1 xor 1 = 0
1 xor 0 = 1
1110 xor 1001 = 0111 (this is equivalent to addition without carry)
And the reference is:
http://www.absoluteastronomy.com/topics/Exclusive_disjunction