英文:
Are these Java long integer or BigInteger format numbers? IP2Location csv database
问题
I have never used long integer or BigInteger format numbers in my Java code, and this aspect of the IP2Location databases does not appear to be documented. I need to know which it is, so that I can write some simple code to compare numbers for greater than or equal. Here are a couple of lines of data from the file...
"281470698522624","281470698524671","CN","China","Guangdong","Guangzhou"
"281470698524672","281470698528767","JP","Japan","Tokyo","Tokyo"
EDIT: 每行开头的两个数字表示位于由该行最后4个条目标识的城市中的IP地址范围。我将我的IP地址转换为已知算法的十进制表示法,然后按顺序在文件中搜索,直到找到一个大于或等于我的IP的第二个数字。然后我有了位置数据
QED
希望能帮到您,
Mick
英文:
I have never used long integer or BigInteger format numbers in my Java code, and this aspect of the IP2Location databases does not appear to be documented. I need to know which it is, so that I can write some simple code to compare numbers for greater that or equal. Here are a couple of lines of data from the file...
"281470698522624","281470698524671","CN","China","Guangdong","Guangzhou"
"281470698524672","281470698528767","JP","Japan","Tokyo","Tokyo"
EDIT: The two numbers at the beginning of each line represent a range of IP addresses that are located in the city identified by the last 4 entries on the line. I convert my IP address to a decimal notation following a known algorithm, then search thru the file sequentially until I find a second number that is greater than or equal to my IP. Then I have the location data
QED
Hope you can help,
Mick
答案1
得分: 1
这是已记录的内容:
ip_from INT(10) / Decimal(39,0) 网段中的第一个IP地址。
ip_to INT(10) / Decimal(39,0) 网段中的最后一个IP地址。
考虑到您的示例包含超过10位数字,您正在使用IPv6版本的DB4。这本应该在您的问题中提到的好事情。这些数字可以变得非常大 - 大于long
可以支持的范围(long
的最大值为2^63-1
,远小于39个九,这是这些数字在理论上可以达到的范围。
您可以使用BigInteger
来表示它们,或者使用2个long
(IPv6地址为128位长,而long
为64位,因此2个long
足够 - 鉴于这是基本的IPv6信息,我认为IP2Location没有必要提到这一点。事实上,将其称为'decimal(39,0)'是一种有点愚蠢的表述'将IPv6地址表示为一个以十进制表示的数字'。无论如何,它们代表InetAddresses,因此您可能希望使用实际描述其内容的类,即InetAddress
。
英文:
It is documented:
ip_from INT(10) / Decimal(39,0) First IP address in netblock.
ip_to INT(10) / Decimal(39,0) Last IP address in netblock.
Given that your samples consist of more than 10 digits, you have the IPv6 version of DB4. Which would have been a good thing to mention in your question. These numbers can grow very large - larger than long
can support (which can go as far as Long.MAX_VALUE
, which is 2^63-1
, far less than 39 nines, which is as far as these numbers can theoretically go.
You could represent them with BigInteger
, or 2 longs (IPv6 addresses are 128 bits long, and longs are 64 bit, so 2 longs can do it - given that this is basic IPv6 info, I don't think it's useful or necessary for IP2Location to mention this. In fact, calling it 'decimal(39,0)' is a bit of a daft way to state 'IPv6 address as a single number stated in decimal'. At any rate, they represent InetAddresses, so you might want to use the class that actually describes what it is, namely, InetAddress
.
答案2
得分: 0
我在这里找到了我需要的一切方法...
IPAddress Java库
https://seancfoley.github.io/IPAddress/
这个库的管理者在一些关于这个主题的帖子中发布了这个链接。
英文:
I found methods for everything that I need in this...
IPAddress Java library
https://seancfoley.github.io/IPAddress/
The manager of this library has posted this link in a few of the threads on this topic.
答案3
得分: -1
这些是Java中的long
值。
在Java中,你可以通过设置MIN_VALUE
和MAX_VALUE
来查看每种类型(Byte、Short、Integer、Long、Float和Double)的最小值和最大值。你可以使用SIZE
来查看它们的位数。
示例:
System.out.println(Long.MAX_VALUE);
System.out.println(Long.SIZE);
输出:
9223372036854775807
64
请查看Double和Float,了解更多常量,如MIN_EXPONENT和PRECISION。
如果需要超出这些类型的要求,请查看BigInteger和BigDecimal。
此外,如果你想找到long
中的最大位数(类似于int, short或byte
),可以使用以下方法:
double digitsInLong = (Long.SIZE*Math.log(2))/Math.log(10);
System.out.println(digitsInLong);
输出:
19.265919722494793
这表示long
可以有19
到20
位数字。所有的long都可以有19
位数字,但一些具有20
位数字的long会超出正数的63
位大小限制。
英文:
You asked:
>Are these Java long integer or BigInteger format numbers?
They are long
values.
In Java, you can see the min
and max
by using setting MIN_VALUE
and MAX_VALUE
for each of Byte, Short, Integer, Long, Float, and Double
. You can see how many bits wide they are by using SIZE.
Examples:
System.out.println(Long.MAX_VALUE);
System.out.println(Long.SIZE);
prints
9223372036854775807
64
Check out Double and Float for more constants such as MIN_EXPONENT and PRECISION.
For requirements exceeding those types, check out BigInteger and BigDecimal
Also, if you want to find the maximum number of digits, in a long
(similar for int, short, or byte
, you can do the following:
double digitsInLong = (Long.SIZE*Math.log(2))/Math.log(10);
System.out.println(digitsInLong);
prints
19.265919722494793
This says there can be between 19
and 20
digits. All longs can have 19
digits but some longs with 20
will overflow the 63
bit size limit for positive numbers.
答案4
得分: -1
您可以使用 Long#parseLong 方法将 String 值转换为 long 基本数据类型。
String[] a = { "281470698522624","281470698524671","CN","China","Guangdong","Guangzhou" };
String[] b = { "281470698524672","281470698528767","JP","Japan","Tokyo","Tokyo" };
long a0 = Long.parseLong(a[0]);
long a1 = Long.parseLong(a[1]);
long b0 = Long.parseLong(b[0]);
long b1 = Long.parseLong(b[1]);
System.out.println("a0 = " + a0);
System.out.println("a1 = " + a1);
System.out.println("b0 = " + b0);
System.out.println("b1 = " + b1);
输出
a0 = 281470698522624
a1 = 281470698524671
b0 = 281470698524672
b1 = 281470698528767
如果您想使用 BigInteger 类,您可以简单地将 String 值作为构造方法的参数。
BigInteger a0 = new BigInteger(a[0]);
BigInteger a1 = new BigInteger(a[1]);
BigInteger b0 = new BigInteger(b[0]);
BigInteger b1 = new BigInteger(b[1]);
英文:
You can use the Long#parseLong method, to convert a String value to a long primitive data-type.
String[] a = { "281470698522624","281470698524671","CN","China","Guangdong","Guangzhou" };
String[] b = { "281470698524672","281470698528767","JP","Japan","Tokyo","Tokyo" };
long a0 = Long.parseLong(a[0]);
long a1 = Long.parseLong(a[1]);
long b0 = Long.parseLong(b[0]);
long b1 = Long.parseLong(b[1]);
System.out.println("a0 = " + a0);
System.out.println("a1 = " + a1);
System.out.println("b0 = " + b0);
System.out.println("b1 = " + b1);
Output
a0 = 281470698522624
a1 = 281470698524671
b0 = 281470698524672
b1 = 281470698528767
If you'd like to use the BigInteger class, you can simply supply the String value as the parameter of the constructor method.
BigInteger a0 = new BigInteger(a[0]);
BigInteger a1 = new BigInteger(a[1]);
BigInteger b0 = new BigInteger(b[0]);
BigInteger b1 = new BigInteger(b[1]);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论