小编lac*_*chs的帖子

Java中的Integer.parseInt(string,radix)返回(大部分)负十进制值

正如我上面所说,我使用Integer.parseInt将十六进制值转换为十进制,但是当我输入正十六进制值时,我一直得到返回的负整数:

byte[] bytes2 = getMacBytes("90:e6:ba:97:4a:bb");

private static byte[] getMacBytes(String macStr) throws IllegalArgumentException {
    byte[] bytes = new byte[6];
    String[] hex = macStr.split("(\\:|\\-)");
    for (int i = 0; i < 6; i++){
        System.out.println(hex[i]);
    }
    if (hex.length != 6) {
        throw new IllegalArgumentException("Invalid MAC address.");
    }
    try {
        for (int i = 0; i < 6; i++) {
            bytes[i] = (byte) Integer.parseInt(hex[i], 16);
            System.out.println(bytes[i]);
        }
    }
    catch (NumberFormatException e) {
        throw new IllegalArgumentException("Invalid hex digit in MAC address.");
    }
    return bytes;
} …
Run Code Online (Sandbox Code Playgroud)

java hex mac-address negative-number

0
推荐指数
1
解决办法
1740
查看次数

标签 统计

hex ×1

java ×1

mac-address ×1

negative-number ×1