繁体   English   中英

如何读取IPv6地址,以完整的扩展表示法格式,然后转换为二进制?

[英]How to read an IPv6 address, format in full expanded notation, then convert to binary?

我正在使用SQL Server 2005。

我试图弄清楚如何读取以字符串形式存储的IPv6地址,然后将其扩展为完整的8个八位位组符号,然后将每个八位位组转换为二进制,然后将所有二进制表示形式加在一起,最后将地址转换为二进制。

这是完整的表示法:2001:0db8:85a3:0000:0000:8a2e:0370:7334与存储在我们数据库中的表示法相同的简称:2001:0db8:85a3 :: 8a2e:0370:7334

这是另一个有效的IPv6地址的示例::: 1

Wikpedia的规则很好地列出并在此处进行了解释: http : //en.wikipedia.org/wiki/IPv6#Address_format

我不确定如何将IPv6地址扩展为完整的符号,并希望这里的某个人已经知道了该地址或知道该怎么做。 我在Google上搜索了几个小时,但找不到SQL Server 2005的任何解决方案。

扩展地址后,我正在考虑根据:拆分每个八位字节,并使用此处概述的方法转换为二进制: http : //blogs.msdn.com/b/sqltips/archive/2008/07/02/从十六进制字符串转换为varbinary和vice-versa.aspx

任何帮助,不胜感激!

SQL小提琴

样品表

select '2001:0db8:85a3:0000:0000:8a2e:0370:7334' address
  into ipv6
  union all
select '2001:db8:85a3::8a2e:0370:7334' union all
select '2001:0db8:85a3::' union all
select '::ff01:0db8:85a3' union all
select '::1' union all
select '002::' union all
select '::' union all
select '1:2:3:4:5:6:7:' union all
select ':A:2:3:4:5:6:7' union all
select 'F:2:3:4:5:6::8' union all
select '' union all -- invalid
select null; -- null test

查询

with step1(address,full8) as(
     select address,
            replace(nullif(address,''),'::',replicate(':',9-len(address)+len(replace(address,':',''))))
       from ipv6
), step2(address,full8,xml) as (
     select address, full8,
            cast('<x>'+replace(full8,':','</x><x>')+'</x>' as xml)
       from step1
), step3(address,full8,xml,part,pos) as (
     select address, full8,xml,
            right('0000'+part.value('.','varchar(4)'),4),
            part.value('for $s in . return count(../*[. << $s]) + 1', 'int') pos
       from step2
cross apply xml.nodes('x') node(part)
), step4(address,hex) as (
     select o.address, (select i.part+''
                          from step3 i
                         where i.address=o.address
                      order by i.pos
                           for xml path('')) hex
       from step3 o
group by address
)
     select address,
            hex,
            cast('' as xml).value(
              'xs:hexBinary(sql:column("hex"))','binary(16)') bin
       from step4
   order by address;

结果

|                                 ADDRESS |                              HEX |                                               BIN |
----------------------------------------------------------------------------------------------------------------------------------
|                                      :: | 00000000000000000000000000000000 |                   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 |
|                                     ::1 | 00000000000000000000000000000001 |                   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 |
|                        ::ff01:0db8:85a3 | 00000000000000000000ff010db885a3 |          0,0,0,0,0,0,0,0,0,0,-1,1,13,-72,-123,-93 |
|                          :A:2:3:4:5:6:7 | 0000000A000200030004000500060007 |                  0,0,0,10,0,2,0,3,0,4,0,5,0,6,0,7 |
|                                   002:: | 00020000000000000000000000000000 |                   0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0 |
|                          1:2:3:4:5:6:7: | 00010002000300040005000600070000 |                   0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,0 |
|                        2001:0db8:85a3:: | 20010db885a300000000000000000000 |          32,1,13,-72,-123,-93,0,0,0,0,0,0,0,0,0,0 |
| 2001:0db8:85a3:0000:0000:8a2e:0370:7334 | 20010db885a3000000008a2e03707334 | 32,1,13,-72,-123,-93,0,0,0,0,-118,46,3,112,115,52 |
|           2001:db8:85a3::8a2e:0370:7334 | 20010db885a3000000008a2e03707334 | 32,1,13,-72,-123,-93,0,0,0,0,-118,46,3,112,115,52 |
|                          F:2:3:4:5:6::8 | 000F0002000300040005000600000008 |                  0,15,0,2,0,3,0,4,0,5,0,6,0,0,0,8 |

注意:SQL Fiddle不能很好地显示二进制数据...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM