62,566
社区成员




import java.io.UnsupportedEncodingException;
public class Byte2String {
public static void main(String args[]) throws UnsupportedEncodingException {
bytetochar();
}
public static void bytetochar() throws UnsupportedEncodingException {
byte addr[] = new byte[] {
(byte) 192, (byte) 168, (byte) 0, (byte) 1};
StringBuffer buff = new StringBuffer();
for (int j = 0; j < addr.length; j++){
buff.append(addr[j] + ".");
}
String sip = buff.toString();
System.out.println(sip);
}
}
希望输出192.168.0.1,却输出-64.-88.0.1.
public static void byte2int() {
byte addr[] = new byte[] {
(byte) 192, (byte) 168, (byte) 0, (byte) 1
};
String s = "";
for (int j = 0; j < addr.length; j++) {
int a = addr[j];
if (a < 0) {
a = 256 + a;
}
s += a + ".";
}
System.out.println(s);
}