public class IPconversion {
public void integertoip(int a){
String b = Integer.toBinaryString(a);
int l = 32 - b.length();
StringBuilder builder = new StringBuilder();
String [] arr = new String[l];
for(int k =0; k<arr.length; k++){
arr[k] = "0";
}
for(int i = 0;i<arr.length;i++){
builder.append(arr[i]);
}
String m = builder.toString();
b = m+b; //adds missing zeros to represent all 32 bits
StringBuilder input1 = new StringBuilder();
input1.append(b);
input1=input1.reverse(); //reverses the 32 bit binary string
System.out.println(input1);
String c = input1.toString();
int len = 8;
String regex = "(?<=^|\\G.{" + len + "})";
String [] parts = c.split (regex); //stores 8 bits chunks in array
for(int i =0; i<parts.length; i++){
StringBuilder input2 = new StringBuilder();
input2.append(parts[i]);
input2=input2.reverse(); //reverses the 8 bits chunks
parts[i] = input2.toString();
System.out.println(parts[i]);
}
for(int g =0; g<parts.length; g++){
double Sum=0;
for(int h=7; h>=0; h-- ){
char s = parts[g].charAt(h); //error on this line
int y = Character.getNumericValue(s);
Sum = Sum+(Math.pow(2,7-h)*y); //computes decimal value
}
Double d = new Double(Sum);
int i = d.intValue();
System.out.println(i+".");
}
}
public static void main(String[] args) {
IPconversion object = new IPconversion();
object.integertoip(167840383);
}
}
所以我试着编写一个将4字节整数转换为点分十进制(ip地址)的代码,但是我收到了这个错误。谁能告诉我为什么会收到此错误?该字符串有8个字符!
输出:
11111110010100001000000001010000
01111111
00001010
00000001
00001010
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 7
at java.lang.String.charAt(String.java:658)
at ipconversion.IPconversion.integertoip(IPconversion.java:66)
at ipconversion.IPconversion.main(IPconversion.java:86)
Java Result: 1
答案 0 :(得分:0)
本规范也适用于我。 (在jdk8 32&amp; 64 bit下运行)
您可以尝试检查以下步骤:
由于某些环境错误,可能是构建路径问题,您的编译器可能无法运行该代码。它发生了什么,它运行了一些最后的良好构建。
编辑:
我没有正则表达式主,但是有线问题。我通过在线编译器运行你的代码,我得到了错误。 由于某种原因,拆分零件什么也没有。 有用的是将代码行更改为:
String [] parts = c.split (Pattern.quote(regex)); //stores 8 bits chunks in array
导入:import java.util.regex.Pattern;
答案 1 :(得分:0)
此代码适用于我。您的JDK版本是什么?它是Java 8吗?
答案 2 :(得分:0)
<Grid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="4" >
<DataGrid x:Name="dgPersonal" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding UserSearchCollection}" RowDetailsVisibilityMode="VisibleWhenSelected" IsReadOnly="True" IsSynchronizedWithCurrentItem="True">
<DataGrid.Columns>
<DataGridTextColumn Width="50" x:Name="RefNo" Binding="{Binding RefNo}" Visibility="Hidden" />
<DataGridTextColumn Header="Name" Width="125" x:Name="FullName" Binding="{Binding FullName, Mode=OneWay}" />
<DataGridTextColumn Header="Phone" Width="75" x:Name="Voice" Binding="{Binding Voice}" />
<DataGridTextColumn Header="Preferred First Name" Width="125" x:Name="FirstName" Binding="{Binding PreferredFirstName}" />
<DataGridTextColumn Header="Preferred Last Name" Width="125" x:Name="LastName" Binding="{Binding PreferredLastName}"/>
<DataGridTextColumn Header="DOS" Width="90" x:Name="DateOfSeparation" Binding="{Binding DateOfSeparation, StringFormat=M/d/yyyy}" SortMemberPath="DateOfSeparation" />
</DataGrid.Columns>
</DataGrid>
</Grid>
所以我改变了第一个for循环的参数,现在它工作正常。问题在于零件阵列的大小,我假设它的大小为4,而它的大小实际上是5!我发现它在第0个索引处没有存储任何实际导致问题的内容!