JamesBowen
19+ years progress programming and still learning.
I have a requirement to convert an IPv6 address into a number (128bit number).
Because it's a 128bit number I will have to store it in the database as a character field.
I found some javascript code which does what I need, but for something so simple I can't figure out what need to do to convert it to ABL Code.
This is what I have but it's not working as expected.
Because it's a 128bit number I will have to store it in the database as a character field.
I found some javascript code which does what I need, but for something so simple I can't figure out what need to do to convert it to ABL Code.
JavaScript:
var ip = '2001:0db8:0:0:8d3:0:0:0';
// 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);
This is what I have but it's not working as expected.
Code:
def var ipAddress as char.
def var part as raw extent .
def var hexPart as character .
def var hexPartTemp as character .
def var partLoop as Integer.
def var base10 as character .
def var ipv6length as Integer.
ipAddress = '2001:0db8:0:0:8d3:0:0:0'.
ipv6length = num-entries(ipAddress, ":").
extent(part) = ipv6length.
do partLoop = 1 to ipv6length:
hexPartTemp = entry(partLoop, ipAddress, ':' ).
hexPart = "0000".
OVERLAY(hexPart, 5 - length(hexPartTemp) ) = hexPartTemp.
message hexPart.
part[partLoop] = hex-decode( hexPart ).
base10 = base10 + STRING( GET-UNSIGNED-SHORT( part[partLoop], 1) ).
end.
message base10.