0

I'm not the best with JavaScript. I was able to get a nice ipv62long() function working in PHP awhile back and was able to help someone with their question regarding it. But posting to PHP just to get a result is pretty nasty in my opinion. Can a function like this be achieved in JavaScript? What functions am I looking for to achieve it? I've tried countless searches and they always feedback to PHP examples.

function ipv62long( $ip ) {
    $binNum = '';
    foreach ( unpack('C*', inet_pton( $ip ) ) as $byte ) {
        $binNum .= str_pad( decbin( $byte ), 8, "0", STR_PAD_LEFT );
    }
    return base_convert( ltrim($binNum, '0' ), 2, 10 );
}

$ip = ipv62long(urldecode('2001%3a0%3a9d38%3a6abd%3a248d%3a2ee4%3a3f57%3afd26'));
echo $ip;
echo '<p>' . long2ip( $ip ) . '</p>';
WASasquatch
  • 1,044
  • 3
  • 11
  • 19
  • 1
    Java is a different language then JavaScript, and PHP @Thomas I did my homework on the search bar too. – WASasquatch Apr 18 '14 at 03:26
  • Language is irrelevant to the issue. – Thomas Apr 18 '14 at 03:27
  • How do you figure, the issue is about doing it in a specific language.... lol – WASasquatch Apr 18 '14 at 03:27
  • 2
    You can't magically convert an ipv6 to ipv4, only IPv6 (4-to-6) IPs. – Thomas Apr 18 '14 at 03:28
  • You are actually using PHP so I am writing it in PHP, almost done...need to do binary work. – Thomas Apr 18 '14 at 04:02
  • Yeah that's what I was originally using, and querying it, but I wanted to do it in JavaScript to make it simpler. Now that I dive deeper into JavaScript, it seems it does not have the libraries to do what I need and I'd have to reinvent the wheel, it seems. Little bit of a disappointment. Querying PHP is fine and all, just hate to use it if it's not necessary. But it looks like it is. – WASasquatch Apr 18 '14 at 04:07
  • Not entirely true, because if you just want ipv4, you just need the last four bytes. – Thomas Apr 18 '14 at 04:11
  • Oh? Hmm, what what functions would you use to delegate from those bytes cause every keeps saying JavaScript lacks the built in functionality and to use another language. – WASasquatch Apr 18 '14 at 04:15
  • 3
    The function you show does not convert IPv6 to IPv4. As has been stated repeatedly, this is impossible. Either you misunderstand what's going on, or you should rewrite your question title, or both. – Michael Hampton Apr 18 '14 at 04:32
  • I just proved you can do it:) Is it the most elegant solution? Nope...but it does work haha. All the bytes are NOTed hence the ~ to invert the bytes. – Thomas Apr 18 '14 at 04:35
  • I made that duplicate question language-agnostic now. – BoltClock Apr 18 '14 at 04:39
  • I have provided a JS example (not great but works) and a PHP example. As stated, only works with Ipv4 to v6 IPs, but fullfills your request. – Thomas Apr 18 '14 at 04:54

1 Answers1

0

Just for kicks I did it in PHP too:)

Explaining the PHP a bit more: I use inet_pton to get a binary value, then by using OR and bit shifting I build a new long values (4 bytes).

Since inet_pton returns a structure, I use ord to get the decimal value of the data.

Then I invert the long using NOT and pass it to long2ip...

<?PHP
$data = inet_pton(urldecode('2001%3a0%3a9d38%3a6abd%3a248d%3a2ee4%3a3f57%3afd26'));
// 192.168.2.217

// build a long 4 bytes using binary ORing
$ip_as_long = ord($data[15]);
$ip_as_long |= ord($data[14]) << 8;
$ip_as_long |= ord($data[13]) << 16;
$ip_as_long |= ord($data[12])<<24;

echo long2ip(~$ip_as_long);
?>

Javascript

 <script>
    var value = '2001:0:9d38:6abd:248d:2ee4:3f57:fd26';

    var split_str = value.split(':');
    value = split_str[6] + split_str[7];

    var ip_1 = ~parseInt(value.substring(0,2),16) & 0xFF;
    var ip_2 =~parseInt(value.substring(2,4),16) & 0xFF;
    var ip_3 =~parseInt(value.substring(4,6),16) & 0xFF;
    var ip_4 =~parseInt(value.substring(6,8),16) & 0xFF;

    alert(ip_1+'.'+ip_2+'.'+ip_3+'.'+ip_4);
    </script>

enjoy.

Thomas
  • 1,401
  • 8
  • 12
  • 2
    Might work with a Teredo address, but it is not applicable to IPv6 generally. – Michael Hampton Apr 18 '14 at 04:51
  • 1
    Yes...we know this smart guy...You can't convert IPv6 to IPv4 otherwise, we already know that, if you read the comments above...this what he wanted! – Thomas Apr 18 '14 at 04:52
  • 2
    @Thomas Calm down and don't be so defensive. StackOverflow comes up as the result of a lot of Google searches. Those people aren't expected to read every comment to figure out what's going on. Such fundamental information should be in the answer itself. – Chris Hayes Apr 19 '14 at 03:12
  • I know, but he downvoted me for an answer that is perfectly applicable. – Thomas Apr 19 '14 at 05:43
  • 2
    @Thomas He never stated he downvoted you, so you're just jumping to conclusions there. And it's entirely possible that if you edited your answer to correct what he pointed out, he'd upvote you, and everyone would be happier. – Chris Hayes Apr 21 '14 at 04:53