ColdFusion IPv6到128位无符号整数 [英] ColdFusion IPv6 to 128-bit unsigned int
问题描述
我需要创建一个可以将IPv6地址转换为其数字表示形式的函数。
I needed to create a function that could convert an IPv6 address to its numeric representation.
使用IPv4非常简单,因为它使用的是32位无符号int为其数字表示形式。 IPv6由128位无符号整数表示。数字的大小太大,无法使用内置ColdFusion位逻辑函数。
Working with IPv4 is pretty straight forward as it uses an 32-bit unsigned int for its numerical representation. IPv6 is represented by an 128-bit unsigned int. That size of a number is too large for the builtin ColdFusion bit logic functions to use.
此函数必须利用底层Java系统进行转换。
This function must make use of the underlying Java system to make the conversion.
需要执行相反操作的函数:将ColdFusion 128位无符号整数转换为IPv6
Need a function to do the reverse: ColdFusion 128-bit unsigned int to IPv6
推荐答案
这是我编写的用于转换将IPv6地址转换为128位无符号整数。
This is the function that I wrote to transform an IPv6 address to a 128-bit unsigned int.
<cffunction name="IPv6ToUInt128" returntype="numeric" output="no" access="public" hint="returns uint128 number for IPv6 address">
<cfargument name="vcIPv6" type="string" required="yes" hint="IPv6 address">
<cfif arguments.vcIPv6 EQ "">
<cfreturn 0>
</cfif>
<cfset local['javaMathBigInteger'] = CreateObject("java", "java.math.BigInteger")>
<cfset local['javaNetInetAddress'] = CreateObject("java", "java.net.InetAddress")>
<cftry>
<cfset local['uint128'] = local.javaMathBigInteger.init(1, local.javaNetInetAddress.getByName(arguments.vcIPv6).getAddress()).toString()>
<cfreturn local.uint128>
<cfcatch type="any">
<cfreturn 0>
</cfcatch>
</cftry>
</cffunction>
如果您有任何改进此代码的建议,请发表评论。
If you have any suggestions to improve this code, please leave comments.
这篇关于ColdFusion IPv6到128位无符号整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!