String s = "ffffffff"; Long i = Long.parseLong(s, 16); System.out.println(Long.toHexString(i)); System.out.println(Long.toBinaryString(i)); System.out.println(i);
我要把它放到byte bytes=new byte[4];中怎么放,请赐教
String s = "ffffffff"; byte[] bytes = s.getBytes();
String s = "ffffffff";
Long i = Long.parseLong(s, 16);
System.out.println(Long.toHexString(i));
System.out.println(Long.toBinaryString(i));
System.out.println(i);
String s = "ffffffff";
byte[] bytes = s.getBytes();
public class Test {
public static int address2Int(String address) {
//参数验证....
return (int)Long.parseLong(address, 16);
}
public static String int2Address(int value) {
return String.format("%08x", value);
}
public static void main(String[] args) {
int value = address2Int("ffffffef");
System.out.println(value);
String address = int2Address(value);
System.out.println(address);
}
}
private final static byte[] hex = "0123456789ABCDEF".getBytes();
private static int parse(char c) {
if (c >= 'a')
return (c - 'a' + 10) & 0x0f;
if (c >= 'A')
return (c - 'A' + 10) & 0x0f;
return (c - '0') & 0x0f;
}
// 从字节数组到十六进制字符串转换
public static String Bytes2HexString(byte[] b) {
byte[] buff = new byte[2 * b.length];
for (int i = 0; i < b.length; i++) {
buff[2 * i] = hex[(b[i] >> 4) & 0x0f];
buff[2 * i + 1] = hex[b[i] & 0x0f];
}
return new String(buff);
}
// 从十六进制字符串到字节数组转换
public static byte[] HexString2Bytes(String hexstr) {
byte[] b = new byte[hexstr.length() / 2];
int j = 0;
for (int i = 0; i < b.length; i++) {
char c0 = hexstr.charAt(j++);
char c1 = hexstr.charAt(j++);
b[i] = (byte) ((parse(c0) << 4) | parse(c1));
}
return b;