Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.7k views
in Technique[技术] by (71.8m points)

javascript - String representation of an ipv6 address in Classic ASP

For geolocation purposes, I am trying to get a string representation of the numeric value of an ip v6 address. Because Classic ASP doesn't handle bigint values, I'm attempting to work around it with a Javascript function.

Based on this working Fiddle: https://jsfiddle.net/adamish/mrx3880p/ that uses the biginteger.js library, I adapted the library to work as a valid include for ASP.

var ip = '2a00:85c0:0001:0000:0000:0000:0241:0023';


// simulate your address.binaryZeroPad(); method
var parts = [];
ip.split(":").forEach(function(it) {
    var bin = parseInt(it, 16).toString(2);
    while (bin.length < 16) {
        bin = "0" + bin;
    }
    parts.push(bin);
})
var bin = parts.join("");

// Use BigInteger library
var dec = bigInt(bin, 2).toString();
console.log(dec);

The code in the Fiddle converts the ipv6 into its binary representation then calls the toString function, requesting the conversion in base 2.

It works in the Fiddle. However, I can't get it to work in my code as the return value is in scientific notation, which is not good for me.

The goal is to input the string "2a00:85c0:1::241:23" (or its non shortened version, "2a00:85c0:0001:0000:0000:0000:0241:0023", doesn't matter) and output a string representation of the numeric equivalent, or "55830288595252163998698714105846497315".

As I am limited to what I can use in Classic ASP, has anyone any pointer on how I can get that conversion to work?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Unfortunately, you won't be able to do this because VBScript and JScript can't handle the numerical precision required.

Here was my first attempt in VBScript

<%
Option Explicit
Const base = 65536
Dim ipv6: ipv6 = "2a00:85c0:0001:0000:0000:0000:0241:0023"
Dim pwr: pwr = 8
Dim hextets: hextets = Split(ipv6, ":")
Dim hextet, ipnum
If IsArray(hextets) Then
  For Each hextet In hextets
    pwr = pwr - 1
    ipnum = ipnum + ((base^pwr) * CLng("&h" & hextet))
  Next
End If
%>
<!doctype html>
<html>
  <head>
    <title>IPv6 to IP Number</title>
  </head>

  <body>
    <pre><%= ipv6 %></pre>
    <pre><%= FormatNumber(ipnum, 0, -2, -2, false) %></pre>
  </body>
</html>

Output:

2a00:85c0:0001:0000:0000:0000:0241:0023
55830288595252200000000000000000000000

The problem here is the calculation is right but the number can't handle the precision so when it reaches the maximum it defaults to 0.

My next attempt was JScript (not my strongest language)

<% @Language = "JScript" %>
<%
var base = 65536;
var ipv6 = "2a00:85c0:0001:0000:0000:0000:0241:0023";
var pwr = 8;
var hextets = ipv6.split(":");
var ipnum;

for (var hextet in hextets) {
  pwr--;
  ipnum += (Math.pow(base, pwr) * parseInt("0x" + hextets[hextet]));
};
%>
<!doctype html>
<html>
  <head>
    <title>IPv6 to IP Number</title>
  </head>

  <body>
    <pre><%= ipv6 %></pre>
    <pre><%= ipnum %></pre>
  </body>
</html>

Output:

2a00:85c0:0001:0000:0000:0000:0241:0023
-1.#IND

This time it fails with a -1.#IND conversion error which basically means the number is too big to be represented.

So while not doable directly in VBScript or JScript you could still build a COM exposed class in another language (C#, VB.Net etc) that did the computation and produced a string that VBScript / Jscript could display.


Useful Links


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...