Conversion IPv6 to long and long to IPv6
How should I perform conversion from IPv6 to long and vice versa?
So far I have:
public static long IPToLong(String addr) {
String addrArray = addr.split("\.");
long num = 0;
for (int i = 0; i < addrArray.length; i++) {
int power = 3 - i;
num += ((Integer.parseInt(addrArray[i], 16) % 256 * Math.pow(256, power)));
}
return num;
}
public static String longToIP(long ip) {
return ((ip >> 24) & 0xFF) + "."
+ ((ip >> 16) & 0xFF) + "."
+ ((ip >> 8) & 0xFF) + "."
+ (ip & 0xFF);
}
Is it correct solution or I missed something?
(It would be perfect if the solution worked for both ipv4 and ipv6)
java ip type-conversion ipv6 long-integer
add a comment |
How should I perform conversion from IPv6 to long and vice versa?
So far I have:
public static long IPToLong(String addr) {
String addrArray = addr.split("\.");
long num = 0;
for (int i = 0; i < addrArray.length; i++) {
int power = 3 - i;
num += ((Integer.parseInt(addrArray[i], 16) % 256 * Math.pow(256, power)));
}
return num;
}
public static String longToIP(long ip) {
return ((ip >> 24) & 0xFF) + "."
+ ((ip >> 16) & 0xFF) + "."
+ ((ip >> 8) & 0xFF) + "."
+ (ip & 0xFF);
}
Is it correct solution or I missed something?
(It would be perfect if the solution worked for both ipv4 and ipv6)
java ip type-conversion ipv6 long-integer
add a comment |
How should I perform conversion from IPv6 to long and vice versa?
So far I have:
public static long IPToLong(String addr) {
String addrArray = addr.split("\.");
long num = 0;
for (int i = 0; i < addrArray.length; i++) {
int power = 3 - i;
num += ((Integer.parseInt(addrArray[i], 16) % 256 * Math.pow(256, power)));
}
return num;
}
public static String longToIP(long ip) {
return ((ip >> 24) & 0xFF) + "."
+ ((ip >> 16) & 0xFF) + "."
+ ((ip >> 8) & 0xFF) + "."
+ (ip & 0xFF);
}
Is it correct solution or I missed something?
(It would be perfect if the solution worked for both ipv4 and ipv6)
java ip type-conversion ipv6 long-integer
How should I perform conversion from IPv6 to long and vice versa?
So far I have:
public static long IPToLong(String addr) {
String addrArray = addr.split("\.");
long num = 0;
for (int i = 0; i < addrArray.length; i++) {
int power = 3 - i;
num += ((Integer.parseInt(addrArray[i], 16) % 256 * Math.pow(256, power)));
}
return num;
}
public static String longToIP(long ip) {
return ((ip >> 24) & 0xFF) + "."
+ ((ip >> 16) & 0xFF) + "."
+ ((ip >> 8) & 0xFF) + "."
+ (ip & 0xFF);
}
Is it correct solution or I missed something?
(It would be perfect if the solution worked for both ipv4 and ipv6)
java ip type-conversion ipv6 long-integer
java ip type-conversion ipv6 long-integer
edited Jul 26 '13 at 8:12
Testeross
asked Jul 26 '13 at 7:45
TesterossTesteross
115119
115119
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
An IPv6 address is a 128-bit number as described here. A long in Java is represented on 64 bits, so you need another structure, like a BigDecimal or two longs (a container with an array of two longs or simply an array of two longs) in order to store an IPv6 address.
Below is an example (just to provide you an idea):
public class Asd {
public static long IPToLong(String addr) {
String addrArray = addr.split(":");//a IPv6 adress is of form 2607:f0d0:1002:0051:0000:0000:0000:0004
long num = new long[addrArray.length];
for (int i=0; i<addrArray.length; i++) {
num[i] = Long.parseLong(addrArray[i], 16);
}
long long1 = num[0];
for (int i=1;i<4;i++) {
long1 = (long1<<16) + num[i];
}
long long2 = num[4];
for (int i=5;i<8;i++) {
long2 = (long2<<16) + num[i];
}
long longs = {long2, long1};
return longs;
}
public static String longToIP(long ip) {
String ipString = "";
for (long crtLong : ip) {//for every long: it should be two of them
for (int i=0; i<4; i++) {//we display in total 4 parts for every long
ipString = Long.toHexString(crtLong & 0xFFFF) + ":" + ipString;
crtLong = crtLong >> 16;
}
}
return ipString;
}
static public void main(String args) {
String ipString = "2607:f0d0:1002:0051:0000:0000:0000:0004";
long asd = IPToLong(ipString);
System.out.println(longToIP(asd));
}
}
Ok, I will do that. What about the conversion? Is it done right?
– Testeross
Jul 26 '13 at 8:27
It is pretty easy to test that: execute longToIP(IPToLong("122.122.122.124")) and you will get "34.34.34.36" instead of the original "122.122.122.124" which means something is not correct.
– Andrei I
Jul 26 '13 at 8:35
You're right. Do you have any idea what is wrong?
– Testeross
Jul 26 '13 at 8:37
The problem is the following: you tried to adapt the code for IPv4 to IPv6, which is incorrect, because in IPv4 the numbers are encoded in base 10 (the number 124 in the IP address "122.122.122.124" is in base 10), but in IPv6 all parts are encoded in hex. This means, you will have to decide somehow what version the IP address is, and then to make some decisions, in order to have one method for both versions.
– Andrei I
Jul 26 '13 at 8:41
1
Added a piece of working code, but do mind that there are missing some checks (like invalid String), so this is ok for your homework, but is not tested enough for production.
– Andrei I
Jul 26 '13 at 9:31
|
show 3 more comments
You can also use java.net.InetAddress
It works with both ipv4 and ipv6 (all formats)
public static BigInteger ipToBigInteger(String addr) {
InetAddress a = InetAddress.getByName(addr)
byte bytes = a.getAddress()
return new BigInteger(1, bytes)
}
3
This will give you negative numbers for the upper half of the IP range. If you want it to be unsigned, you need to pass in the signum to keep it positive valued (ie new BigInteger(1, bytes)).
– OTrain
Oct 20 '16 at 20:18
1
@OTrain Thanks for the comment. Response updated.
– Guigoz
Oct 21 '16 at 11:00
add a comment |
An IPv6 address can not be stored in long. You can use BigInteger instead of long.
public static BigInteger ipv6ToNumber(String addr) {
int startIndex=addr.indexOf("::");
if(startIndex!=-1){
String firstStr=addr.substring(0,startIndex);
String secondStr=addr.substring(startIndex+2, addr.length());
BigInteger first=ipv6ToNumber(firstStr);
int x=countChar(addr, ':');
first=first.shiftLeft(16*(7-x)).add(ipv6ToNumber(secondStr));
return first;
}
String strArr = addr.split(":");
BigInteger retValue = BigInteger.valueOf(0);
for (int i=0;i<strArr.length;i++) {
BigInteger bi=new BigInteger(strArr[i], 16);
retValue = retValue.shiftLeft(16).add(bi);
}
return retValue;
}
public static String numberToIPv6(BigInteger ipNumber) {
String ipString ="";
BigInteger a=new BigInteger("FFFF", 16);
for (int i=0; i<8; i++) {
ipString=ipNumber.and(a).toString(16)+":"+ipString;
ipNumber = ipNumber.shiftRight(16);
}
return ipString.substring(0, ipString.length()-1);
}
public static int countChar(String str, char reg){
char ch=str.toCharArray();
int count=0;
for(int i=0; i<ch.length; ++i){
if(ch[i]==reg){
if(ch[i+1]==reg){
++i;
continue;
}
++count;
}
}
return count;
}
add a comment |
Vinod's answer is right. But there are still some things that can be improved.(Sorry I can not add a comment below Vinod's answer because I am new here.)
First, in method 'countChar', 'continue' should be replaced by 'break'.
And second, Some boundary conditions must be considered.
public static BigInteger ipv6ToNumber(String addr) {
int startIndex = addr.indexOf("::");
if (startIndex != -1) {
String firstStr = addr.substring(0, startIndex);
String secondStr = addr.substring(startIndex + 2, addr.length());
BigInteger first = new BigInteger("0");
BigInteger second = new BigInteger("0");
if (!firstStr.equals("")) {
int x = countChar(addr, ':');
first = ipv6ToNumber(firstStr).shiftLeft(16 * (7 - x));
}
if (!secondStr.equals("")) {
second = ipv6ToNumber(secondStr);
}
first = first.add(second);
return first;
}
String strArr = addr.split(":");
BigInteger retValue = BigInteger.valueOf(0);
for (int i = 0; i < strArr.length; i++) {
BigInteger bi = new BigInteger(strArr[i], 16);
retValue = retValue.shiftLeft(16).add(bi);
}
return retValue;
}
public static int countChar(String str, char reg){
char ch=str.toCharArray();
int count=0;
for(int i=0; i<ch.length; ++i){
if(ch[i]==reg){
if(ch[i+1]==reg){
++i;
break;
}
++count;
}
}
return count;
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17875728%2fconversion-ipv6-to-long-and-long-to-ipv6%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
An IPv6 address is a 128-bit number as described here. A long in Java is represented on 64 bits, so you need another structure, like a BigDecimal or two longs (a container with an array of two longs or simply an array of two longs) in order to store an IPv6 address.
Below is an example (just to provide you an idea):
public class Asd {
public static long IPToLong(String addr) {
String addrArray = addr.split(":");//a IPv6 adress is of form 2607:f0d0:1002:0051:0000:0000:0000:0004
long num = new long[addrArray.length];
for (int i=0; i<addrArray.length; i++) {
num[i] = Long.parseLong(addrArray[i], 16);
}
long long1 = num[0];
for (int i=1;i<4;i++) {
long1 = (long1<<16) + num[i];
}
long long2 = num[4];
for (int i=5;i<8;i++) {
long2 = (long2<<16) + num[i];
}
long longs = {long2, long1};
return longs;
}
public static String longToIP(long ip) {
String ipString = "";
for (long crtLong : ip) {//for every long: it should be two of them
for (int i=0; i<4; i++) {//we display in total 4 parts for every long
ipString = Long.toHexString(crtLong & 0xFFFF) + ":" + ipString;
crtLong = crtLong >> 16;
}
}
return ipString;
}
static public void main(String args) {
String ipString = "2607:f0d0:1002:0051:0000:0000:0000:0004";
long asd = IPToLong(ipString);
System.out.println(longToIP(asd));
}
}
Ok, I will do that. What about the conversion? Is it done right?
– Testeross
Jul 26 '13 at 8:27
It is pretty easy to test that: execute longToIP(IPToLong("122.122.122.124")) and you will get "34.34.34.36" instead of the original "122.122.122.124" which means something is not correct.
– Andrei I
Jul 26 '13 at 8:35
You're right. Do you have any idea what is wrong?
– Testeross
Jul 26 '13 at 8:37
The problem is the following: you tried to adapt the code for IPv4 to IPv6, which is incorrect, because in IPv4 the numbers are encoded in base 10 (the number 124 in the IP address "122.122.122.124" is in base 10), but in IPv6 all parts are encoded in hex. This means, you will have to decide somehow what version the IP address is, and then to make some decisions, in order to have one method for both versions.
– Andrei I
Jul 26 '13 at 8:41
1
Added a piece of working code, but do mind that there are missing some checks (like invalid String), so this is ok for your homework, but is not tested enough for production.
– Andrei I
Jul 26 '13 at 9:31
|
show 3 more comments
An IPv6 address is a 128-bit number as described here. A long in Java is represented on 64 bits, so you need another structure, like a BigDecimal or two longs (a container with an array of two longs or simply an array of two longs) in order to store an IPv6 address.
Below is an example (just to provide you an idea):
public class Asd {
public static long IPToLong(String addr) {
String addrArray = addr.split(":");//a IPv6 adress is of form 2607:f0d0:1002:0051:0000:0000:0000:0004
long num = new long[addrArray.length];
for (int i=0; i<addrArray.length; i++) {
num[i] = Long.parseLong(addrArray[i], 16);
}
long long1 = num[0];
for (int i=1;i<4;i++) {
long1 = (long1<<16) + num[i];
}
long long2 = num[4];
for (int i=5;i<8;i++) {
long2 = (long2<<16) + num[i];
}
long longs = {long2, long1};
return longs;
}
public static String longToIP(long ip) {
String ipString = "";
for (long crtLong : ip) {//for every long: it should be two of them
for (int i=0; i<4; i++) {//we display in total 4 parts for every long
ipString = Long.toHexString(crtLong & 0xFFFF) + ":" + ipString;
crtLong = crtLong >> 16;
}
}
return ipString;
}
static public void main(String args) {
String ipString = "2607:f0d0:1002:0051:0000:0000:0000:0004";
long asd = IPToLong(ipString);
System.out.println(longToIP(asd));
}
}
Ok, I will do that. What about the conversion? Is it done right?
– Testeross
Jul 26 '13 at 8:27
It is pretty easy to test that: execute longToIP(IPToLong("122.122.122.124")) and you will get "34.34.34.36" instead of the original "122.122.122.124" which means something is not correct.
– Andrei I
Jul 26 '13 at 8:35
You're right. Do you have any idea what is wrong?
– Testeross
Jul 26 '13 at 8:37
The problem is the following: you tried to adapt the code for IPv4 to IPv6, which is incorrect, because in IPv4 the numbers are encoded in base 10 (the number 124 in the IP address "122.122.122.124" is in base 10), but in IPv6 all parts are encoded in hex. This means, you will have to decide somehow what version the IP address is, and then to make some decisions, in order to have one method for both versions.
– Andrei I
Jul 26 '13 at 8:41
1
Added a piece of working code, but do mind that there are missing some checks (like invalid String), so this is ok for your homework, but is not tested enough for production.
– Andrei I
Jul 26 '13 at 9:31
|
show 3 more comments
An IPv6 address is a 128-bit number as described here. A long in Java is represented on 64 bits, so you need another structure, like a BigDecimal or two longs (a container with an array of two longs or simply an array of two longs) in order to store an IPv6 address.
Below is an example (just to provide you an idea):
public class Asd {
public static long IPToLong(String addr) {
String addrArray = addr.split(":");//a IPv6 adress is of form 2607:f0d0:1002:0051:0000:0000:0000:0004
long num = new long[addrArray.length];
for (int i=0; i<addrArray.length; i++) {
num[i] = Long.parseLong(addrArray[i], 16);
}
long long1 = num[0];
for (int i=1;i<4;i++) {
long1 = (long1<<16) + num[i];
}
long long2 = num[4];
for (int i=5;i<8;i++) {
long2 = (long2<<16) + num[i];
}
long longs = {long2, long1};
return longs;
}
public static String longToIP(long ip) {
String ipString = "";
for (long crtLong : ip) {//for every long: it should be two of them
for (int i=0; i<4; i++) {//we display in total 4 parts for every long
ipString = Long.toHexString(crtLong & 0xFFFF) + ":" + ipString;
crtLong = crtLong >> 16;
}
}
return ipString;
}
static public void main(String args) {
String ipString = "2607:f0d0:1002:0051:0000:0000:0000:0004";
long asd = IPToLong(ipString);
System.out.println(longToIP(asd));
}
}
An IPv6 address is a 128-bit number as described here. A long in Java is represented on 64 bits, so you need another structure, like a BigDecimal or two longs (a container with an array of two longs or simply an array of two longs) in order to store an IPv6 address.
Below is an example (just to provide you an idea):
public class Asd {
public static long IPToLong(String addr) {
String addrArray = addr.split(":");//a IPv6 adress is of form 2607:f0d0:1002:0051:0000:0000:0000:0004
long num = new long[addrArray.length];
for (int i=0; i<addrArray.length; i++) {
num[i] = Long.parseLong(addrArray[i], 16);
}
long long1 = num[0];
for (int i=1;i<4;i++) {
long1 = (long1<<16) + num[i];
}
long long2 = num[4];
for (int i=5;i<8;i++) {
long2 = (long2<<16) + num[i];
}
long longs = {long2, long1};
return longs;
}
public static String longToIP(long ip) {
String ipString = "";
for (long crtLong : ip) {//for every long: it should be two of them
for (int i=0; i<4; i++) {//we display in total 4 parts for every long
ipString = Long.toHexString(crtLong & 0xFFFF) + ":" + ipString;
crtLong = crtLong >> 16;
}
}
return ipString;
}
static public void main(String args) {
String ipString = "2607:f0d0:1002:0051:0000:0000:0000:0004";
long asd = IPToLong(ipString);
System.out.println(longToIP(asd));
}
}
edited Mar 16 '16 at 11:42
corny
4,3783919
4,3783919
answered Jul 26 '13 at 8:20
Andrei IAndrei I
16.2k63776
16.2k63776
Ok, I will do that. What about the conversion? Is it done right?
– Testeross
Jul 26 '13 at 8:27
It is pretty easy to test that: execute longToIP(IPToLong("122.122.122.124")) and you will get "34.34.34.36" instead of the original "122.122.122.124" which means something is not correct.
– Andrei I
Jul 26 '13 at 8:35
You're right. Do you have any idea what is wrong?
– Testeross
Jul 26 '13 at 8:37
The problem is the following: you tried to adapt the code for IPv4 to IPv6, which is incorrect, because in IPv4 the numbers are encoded in base 10 (the number 124 in the IP address "122.122.122.124" is in base 10), but in IPv6 all parts are encoded in hex. This means, you will have to decide somehow what version the IP address is, and then to make some decisions, in order to have one method for both versions.
– Andrei I
Jul 26 '13 at 8:41
1
Added a piece of working code, but do mind that there are missing some checks (like invalid String), so this is ok for your homework, but is not tested enough for production.
– Andrei I
Jul 26 '13 at 9:31
|
show 3 more comments
Ok, I will do that. What about the conversion? Is it done right?
– Testeross
Jul 26 '13 at 8:27
It is pretty easy to test that: execute longToIP(IPToLong("122.122.122.124")) and you will get "34.34.34.36" instead of the original "122.122.122.124" which means something is not correct.
– Andrei I
Jul 26 '13 at 8:35
You're right. Do you have any idea what is wrong?
– Testeross
Jul 26 '13 at 8:37
The problem is the following: you tried to adapt the code for IPv4 to IPv6, which is incorrect, because in IPv4 the numbers are encoded in base 10 (the number 124 in the IP address "122.122.122.124" is in base 10), but in IPv6 all parts are encoded in hex. This means, you will have to decide somehow what version the IP address is, and then to make some decisions, in order to have one method for both versions.
– Andrei I
Jul 26 '13 at 8:41
1
Added a piece of working code, but do mind that there are missing some checks (like invalid String), so this is ok for your homework, but is not tested enough for production.
– Andrei I
Jul 26 '13 at 9:31
Ok, I will do that. What about the conversion? Is it done right?
– Testeross
Jul 26 '13 at 8:27
Ok, I will do that. What about the conversion? Is it done right?
– Testeross
Jul 26 '13 at 8:27
It is pretty easy to test that: execute longToIP(IPToLong("122.122.122.124")) and you will get "34.34.34.36" instead of the original "122.122.122.124" which means something is not correct.
– Andrei I
Jul 26 '13 at 8:35
It is pretty easy to test that: execute longToIP(IPToLong("122.122.122.124")) and you will get "34.34.34.36" instead of the original "122.122.122.124" which means something is not correct.
– Andrei I
Jul 26 '13 at 8:35
You're right. Do you have any idea what is wrong?
– Testeross
Jul 26 '13 at 8:37
You're right. Do you have any idea what is wrong?
– Testeross
Jul 26 '13 at 8:37
The problem is the following: you tried to adapt the code for IPv4 to IPv6, which is incorrect, because in IPv4 the numbers are encoded in base 10 (the number 124 in the IP address "122.122.122.124" is in base 10), but in IPv6 all parts are encoded in hex. This means, you will have to decide somehow what version the IP address is, and then to make some decisions, in order to have one method for both versions.
– Andrei I
Jul 26 '13 at 8:41
The problem is the following: you tried to adapt the code for IPv4 to IPv6, which is incorrect, because in IPv4 the numbers are encoded in base 10 (the number 124 in the IP address "122.122.122.124" is in base 10), but in IPv6 all parts are encoded in hex. This means, you will have to decide somehow what version the IP address is, and then to make some decisions, in order to have one method for both versions.
– Andrei I
Jul 26 '13 at 8:41
1
1
Added a piece of working code, but do mind that there are missing some checks (like invalid String), so this is ok for your homework, but is not tested enough for production.
– Andrei I
Jul 26 '13 at 9:31
Added a piece of working code, but do mind that there are missing some checks (like invalid String), so this is ok for your homework, but is not tested enough for production.
– Andrei I
Jul 26 '13 at 9:31
|
show 3 more comments
You can also use java.net.InetAddress
It works with both ipv4 and ipv6 (all formats)
public static BigInteger ipToBigInteger(String addr) {
InetAddress a = InetAddress.getByName(addr)
byte bytes = a.getAddress()
return new BigInteger(1, bytes)
}
3
This will give you negative numbers for the upper half of the IP range. If you want it to be unsigned, you need to pass in the signum to keep it positive valued (ie new BigInteger(1, bytes)).
– OTrain
Oct 20 '16 at 20:18
1
@OTrain Thanks for the comment. Response updated.
– Guigoz
Oct 21 '16 at 11:00
add a comment |
You can also use java.net.InetAddress
It works with both ipv4 and ipv6 (all formats)
public static BigInteger ipToBigInteger(String addr) {
InetAddress a = InetAddress.getByName(addr)
byte bytes = a.getAddress()
return new BigInteger(1, bytes)
}
3
This will give you negative numbers for the upper half of the IP range. If you want it to be unsigned, you need to pass in the signum to keep it positive valued (ie new BigInteger(1, bytes)).
– OTrain
Oct 20 '16 at 20:18
1
@OTrain Thanks for the comment. Response updated.
– Guigoz
Oct 21 '16 at 11:00
add a comment |
You can also use java.net.InetAddress
It works with both ipv4 and ipv6 (all formats)
public static BigInteger ipToBigInteger(String addr) {
InetAddress a = InetAddress.getByName(addr)
byte bytes = a.getAddress()
return new BigInteger(1, bytes)
}
You can also use java.net.InetAddress
It works with both ipv4 and ipv6 (all formats)
public static BigInteger ipToBigInteger(String addr) {
InetAddress a = InetAddress.getByName(addr)
byte bytes = a.getAddress()
return new BigInteger(1, bytes)
}
edited Oct 21 '16 at 10:59
answered Jan 19 '16 at 15:57
GuigozGuigoz
309413
309413
3
This will give you negative numbers for the upper half of the IP range. If you want it to be unsigned, you need to pass in the signum to keep it positive valued (ie new BigInteger(1, bytes)).
– OTrain
Oct 20 '16 at 20:18
1
@OTrain Thanks for the comment. Response updated.
– Guigoz
Oct 21 '16 at 11:00
add a comment |
3
This will give you negative numbers for the upper half of the IP range. If you want it to be unsigned, you need to pass in the signum to keep it positive valued (ie new BigInteger(1, bytes)).
– OTrain
Oct 20 '16 at 20:18
1
@OTrain Thanks for the comment. Response updated.
– Guigoz
Oct 21 '16 at 11:00
3
3
This will give you negative numbers for the upper half of the IP range. If you want it to be unsigned, you need to pass in the signum to keep it positive valued (ie new BigInteger(1, bytes)).
– OTrain
Oct 20 '16 at 20:18
This will give you negative numbers for the upper half of the IP range. If you want it to be unsigned, you need to pass in the signum to keep it positive valued (ie new BigInteger(1, bytes)).
– OTrain
Oct 20 '16 at 20:18
1
1
@OTrain Thanks for the comment. Response updated.
– Guigoz
Oct 21 '16 at 11:00
@OTrain Thanks for the comment. Response updated.
– Guigoz
Oct 21 '16 at 11:00
add a comment |
An IPv6 address can not be stored in long. You can use BigInteger instead of long.
public static BigInteger ipv6ToNumber(String addr) {
int startIndex=addr.indexOf("::");
if(startIndex!=-1){
String firstStr=addr.substring(0,startIndex);
String secondStr=addr.substring(startIndex+2, addr.length());
BigInteger first=ipv6ToNumber(firstStr);
int x=countChar(addr, ':');
first=first.shiftLeft(16*(7-x)).add(ipv6ToNumber(secondStr));
return first;
}
String strArr = addr.split(":");
BigInteger retValue = BigInteger.valueOf(0);
for (int i=0;i<strArr.length;i++) {
BigInteger bi=new BigInteger(strArr[i], 16);
retValue = retValue.shiftLeft(16).add(bi);
}
return retValue;
}
public static String numberToIPv6(BigInteger ipNumber) {
String ipString ="";
BigInteger a=new BigInteger("FFFF", 16);
for (int i=0; i<8; i++) {
ipString=ipNumber.and(a).toString(16)+":"+ipString;
ipNumber = ipNumber.shiftRight(16);
}
return ipString.substring(0, ipString.length()-1);
}
public static int countChar(String str, char reg){
char ch=str.toCharArray();
int count=0;
for(int i=0; i<ch.length; ++i){
if(ch[i]==reg){
if(ch[i+1]==reg){
++i;
continue;
}
++count;
}
}
return count;
}
add a comment |
An IPv6 address can not be stored in long. You can use BigInteger instead of long.
public static BigInteger ipv6ToNumber(String addr) {
int startIndex=addr.indexOf("::");
if(startIndex!=-1){
String firstStr=addr.substring(0,startIndex);
String secondStr=addr.substring(startIndex+2, addr.length());
BigInteger first=ipv6ToNumber(firstStr);
int x=countChar(addr, ':');
first=first.shiftLeft(16*(7-x)).add(ipv6ToNumber(secondStr));
return first;
}
String strArr = addr.split(":");
BigInteger retValue = BigInteger.valueOf(0);
for (int i=0;i<strArr.length;i++) {
BigInteger bi=new BigInteger(strArr[i], 16);
retValue = retValue.shiftLeft(16).add(bi);
}
return retValue;
}
public static String numberToIPv6(BigInteger ipNumber) {
String ipString ="";
BigInteger a=new BigInteger("FFFF", 16);
for (int i=0; i<8; i++) {
ipString=ipNumber.and(a).toString(16)+":"+ipString;
ipNumber = ipNumber.shiftRight(16);
}
return ipString.substring(0, ipString.length()-1);
}
public static int countChar(String str, char reg){
char ch=str.toCharArray();
int count=0;
for(int i=0; i<ch.length; ++i){
if(ch[i]==reg){
if(ch[i+1]==reg){
++i;
continue;
}
++count;
}
}
return count;
}
add a comment |
An IPv6 address can not be stored in long. You can use BigInteger instead of long.
public static BigInteger ipv6ToNumber(String addr) {
int startIndex=addr.indexOf("::");
if(startIndex!=-1){
String firstStr=addr.substring(0,startIndex);
String secondStr=addr.substring(startIndex+2, addr.length());
BigInteger first=ipv6ToNumber(firstStr);
int x=countChar(addr, ':');
first=first.shiftLeft(16*(7-x)).add(ipv6ToNumber(secondStr));
return first;
}
String strArr = addr.split(":");
BigInteger retValue = BigInteger.valueOf(0);
for (int i=0;i<strArr.length;i++) {
BigInteger bi=new BigInteger(strArr[i], 16);
retValue = retValue.shiftLeft(16).add(bi);
}
return retValue;
}
public static String numberToIPv6(BigInteger ipNumber) {
String ipString ="";
BigInteger a=new BigInteger("FFFF", 16);
for (int i=0; i<8; i++) {
ipString=ipNumber.and(a).toString(16)+":"+ipString;
ipNumber = ipNumber.shiftRight(16);
}
return ipString.substring(0, ipString.length()-1);
}
public static int countChar(String str, char reg){
char ch=str.toCharArray();
int count=0;
for(int i=0; i<ch.length; ++i){
if(ch[i]==reg){
if(ch[i+1]==reg){
++i;
continue;
}
++count;
}
}
return count;
}
An IPv6 address can not be stored in long. You can use BigInteger instead of long.
public static BigInteger ipv6ToNumber(String addr) {
int startIndex=addr.indexOf("::");
if(startIndex!=-1){
String firstStr=addr.substring(0,startIndex);
String secondStr=addr.substring(startIndex+2, addr.length());
BigInteger first=ipv6ToNumber(firstStr);
int x=countChar(addr, ':');
first=first.shiftLeft(16*(7-x)).add(ipv6ToNumber(secondStr));
return first;
}
String strArr = addr.split(":");
BigInteger retValue = BigInteger.valueOf(0);
for (int i=0;i<strArr.length;i++) {
BigInteger bi=new BigInteger(strArr[i], 16);
retValue = retValue.shiftLeft(16).add(bi);
}
return retValue;
}
public static String numberToIPv6(BigInteger ipNumber) {
String ipString ="";
BigInteger a=new BigInteger("FFFF", 16);
for (int i=0; i<8; i++) {
ipString=ipNumber.and(a).toString(16)+":"+ipString;
ipNumber = ipNumber.shiftRight(16);
}
return ipString.substring(0, ipString.length()-1);
}
public static int countChar(String str, char reg){
char ch=str.toCharArray();
int count=0;
for(int i=0; i<ch.length; ++i){
if(ch[i]==reg){
if(ch[i+1]==reg){
++i;
continue;
}
++count;
}
}
return count;
}
answered Oct 8 '13 at 4:15
VinodVinod
69621228
69621228
add a comment |
add a comment |
Vinod's answer is right. But there are still some things that can be improved.(Sorry I can not add a comment below Vinod's answer because I am new here.)
First, in method 'countChar', 'continue' should be replaced by 'break'.
And second, Some boundary conditions must be considered.
public static BigInteger ipv6ToNumber(String addr) {
int startIndex = addr.indexOf("::");
if (startIndex != -1) {
String firstStr = addr.substring(0, startIndex);
String secondStr = addr.substring(startIndex + 2, addr.length());
BigInteger first = new BigInteger("0");
BigInteger second = new BigInteger("0");
if (!firstStr.equals("")) {
int x = countChar(addr, ':');
first = ipv6ToNumber(firstStr).shiftLeft(16 * (7 - x));
}
if (!secondStr.equals("")) {
second = ipv6ToNumber(secondStr);
}
first = first.add(second);
return first;
}
String strArr = addr.split(":");
BigInteger retValue = BigInteger.valueOf(0);
for (int i = 0; i < strArr.length; i++) {
BigInteger bi = new BigInteger(strArr[i], 16);
retValue = retValue.shiftLeft(16).add(bi);
}
return retValue;
}
public static int countChar(String str, char reg){
char ch=str.toCharArray();
int count=0;
for(int i=0; i<ch.length; ++i){
if(ch[i]==reg){
if(ch[i+1]==reg){
++i;
break;
}
++count;
}
}
return count;
}
add a comment |
Vinod's answer is right. But there are still some things that can be improved.(Sorry I can not add a comment below Vinod's answer because I am new here.)
First, in method 'countChar', 'continue' should be replaced by 'break'.
And second, Some boundary conditions must be considered.
public static BigInteger ipv6ToNumber(String addr) {
int startIndex = addr.indexOf("::");
if (startIndex != -1) {
String firstStr = addr.substring(0, startIndex);
String secondStr = addr.substring(startIndex + 2, addr.length());
BigInteger first = new BigInteger("0");
BigInteger second = new BigInteger("0");
if (!firstStr.equals("")) {
int x = countChar(addr, ':');
first = ipv6ToNumber(firstStr).shiftLeft(16 * (7 - x));
}
if (!secondStr.equals("")) {
second = ipv6ToNumber(secondStr);
}
first = first.add(second);
return first;
}
String strArr = addr.split(":");
BigInteger retValue = BigInteger.valueOf(0);
for (int i = 0; i < strArr.length; i++) {
BigInteger bi = new BigInteger(strArr[i], 16);
retValue = retValue.shiftLeft(16).add(bi);
}
return retValue;
}
public static int countChar(String str, char reg){
char ch=str.toCharArray();
int count=0;
for(int i=0; i<ch.length; ++i){
if(ch[i]==reg){
if(ch[i+1]==reg){
++i;
break;
}
++count;
}
}
return count;
}
add a comment |
Vinod's answer is right. But there are still some things that can be improved.(Sorry I can not add a comment below Vinod's answer because I am new here.)
First, in method 'countChar', 'continue' should be replaced by 'break'.
And second, Some boundary conditions must be considered.
public static BigInteger ipv6ToNumber(String addr) {
int startIndex = addr.indexOf("::");
if (startIndex != -1) {
String firstStr = addr.substring(0, startIndex);
String secondStr = addr.substring(startIndex + 2, addr.length());
BigInteger first = new BigInteger("0");
BigInteger second = new BigInteger("0");
if (!firstStr.equals("")) {
int x = countChar(addr, ':');
first = ipv6ToNumber(firstStr).shiftLeft(16 * (7 - x));
}
if (!secondStr.equals("")) {
second = ipv6ToNumber(secondStr);
}
first = first.add(second);
return first;
}
String strArr = addr.split(":");
BigInteger retValue = BigInteger.valueOf(0);
for (int i = 0; i < strArr.length; i++) {
BigInteger bi = new BigInteger(strArr[i], 16);
retValue = retValue.shiftLeft(16).add(bi);
}
return retValue;
}
public static int countChar(String str, char reg){
char ch=str.toCharArray();
int count=0;
for(int i=0; i<ch.length; ++i){
if(ch[i]==reg){
if(ch[i+1]==reg){
++i;
break;
}
++count;
}
}
return count;
}
Vinod's answer is right. But there are still some things that can be improved.(Sorry I can not add a comment below Vinod's answer because I am new here.)
First, in method 'countChar', 'continue' should be replaced by 'break'.
And second, Some boundary conditions must be considered.
public static BigInteger ipv6ToNumber(String addr) {
int startIndex = addr.indexOf("::");
if (startIndex != -1) {
String firstStr = addr.substring(0, startIndex);
String secondStr = addr.substring(startIndex + 2, addr.length());
BigInteger first = new BigInteger("0");
BigInteger second = new BigInteger("0");
if (!firstStr.equals("")) {
int x = countChar(addr, ':');
first = ipv6ToNumber(firstStr).shiftLeft(16 * (7 - x));
}
if (!secondStr.equals("")) {
second = ipv6ToNumber(secondStr);
}
first = first.add(second);
return first;
}
String strArr = addr.split(":");
BigInteger retValue = BigInteger.valueOf(0);
for (int i = 0; i < strArr.length; i++) {
BigInteger bi = new BigInteger(strArr[i], 16);
retValue = retValue.shiftLeft(16).add(bi);
}
return retValue;
}
public static int countChar(String str, char reg){
char ch=str.toCharArray();
int count=0;
for(int i=0; i<ch.length; ++i){
if(ch[i]==reg){
if(ch[i+1]==reg){
++i;
break;
}
++count;
}
}
return count;
}
edited Nov 28 '18 at 3:36
answered Nov 28 '18 at 3:10
gaofcgaofc
112
112
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17875728%2fconversion-ipv6-to-long-and-long-to-ipv6%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown