IPv6 地址的算术(大整数)

发布于 2024-07-17 00:37:32 字数 258 浏览 5 评论 0原文

我正在使用以下形式的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

安静被遗忘 2024-07-24 00:37:32

Jes Klinke 在此处为 Pascal 编写了一个 bignum 单元。

免责声明:我个人没有使用过这个库。

Jes Klinke wrote a bignum unit for Pascal here.

Disclaimer : I have not used this library personally.

人事已非 2024-07-24 00:37:32

在尝试了许多建议之后,我找不到一个能够满足我所有需求并且没有错误的库。 我进行了一些搜索,发现了 Alex Ciobanu 的一个相对较新的库,它无缝地处理 BigIntegers(和 Big Cardinals),允许您以与操作普通整数、基数等相同的方式操作它们。

除了 BigIntegers 之外,该库还提供了许多非常有用的功能。 来自自述文件:

  • 一组通用集合类
    (列表、字典、哈希集等)。
  • 日期/时间功能全部组合在一起
    在一些结构中(以某种方式
    相当于.NET的DateTime
    结构)
  • 类型支持概念,定义了
    一组默认的“支持类”
    每个内置的 Delphi 类型(用作
    集合中的默认值)。 风俗
    “类型支持”类可以是
    注册您的自定义数据
    类型。
  • BigCardinal 和 BigInteger 数据类型。
  • 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:

  • A set of generic collections classes
    (List, Dictionary, HashSet, etc).
  • Date/Time functionality all combined
    in a few structures (somehow
    equivalent to .NET's DateTime
    structure)
  • Type Support concept that defines a
    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.
  • BigCardinal and BigInteger data types.
  • Smart pointers in Delphi

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.

昨迟人 2024-07-24 00:37:32

我曾经编写过一个 IPv4 和 IPv6 转换单元,其中包括自定义变体类型对于两种类型的 IP 地址。

例如,使用这些 Variant 类型,可以进行以下示例算术和转换:

procedure TForm1.Button1Click(Sender: TObject);
var
  I4: TIPv4;
  I6: TIPv6;
  V1, V2, V3, V4: Variant;
begin
  I4 := StrToIPv4('192.0.2.128');
  I6 := IPv4ToIPv6(I4);
  V1 := VarIPv6Create('2001:db8:85a3:0:0:8a2e:0370:7334');
  V2 := IPv6ToVar(I6);
  V3 := V1 - V2;
  V4 := V1 or V2;
  if V3 < V4 then
    Log(V3 + ' is smaller than ' + V4);
  if V2.Equals('::ffff:192.0.2.128') or V2.IsZero then
    Log('OK');
  Log('V1 = ' + V1.AsStringOutwritten);
  Log('V2 = ' + V2.AsURL);
  Log('V3 = ' + V3.AsStringCompressed);
  V4.Follow;
end;

procedure TForm1.Log(const S: String);
begin
  Memo.Lines.Add(S);
end;

自定义变体类型确实非常强大。

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:

procedure TForm1.Button1Click(Sender: TObject);
var
  I4: TIPv4;
  I6: TIPv6;
  V1, V2, V3, V4: Variant;
begin
  I4 := StrToIPv4('192.0.2.128');
  I6 := IPv4ToIPv6(I4);
  V1 := VarIPv6Create('2001:db8:85a3:0:0:8a2e:0370:7334');
  V2 := IPv6ToVar(I6);
  V3 := V1 - V2;
  V4 := V1 or V2;
  if V3 < V4 then
    Log(V3 + ' is smaller than ' + V4);
  if V2.Equals('::ffff:192.0.2.128') or V2.IsZero then
    Log('OK');
  Log('V1 = ' + V1.AsStringOutwritten);
  Log('V2 = ' + V2.AsURL);
  Log('V3 = ' + V3.AsStringCompressed);
  V4.Follow;
end;

procedure TForm1.Log(const S: String);
begin
  Memo.Lines.Add(S);
end;

Custom variant types really are quite powerfull.

日记撕了你也走了 2024-07-24 00:37:32

我想说的是,如果你会加法,你就可以用它来用加法进行减法、乘法和除法。 我是否应该假设溢出将被简单地忽略?

我似乎记得使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文