Программа преобразования начальных чисел (двоичная в числовую, шестнадцатеричная в числовую) Вопрос реализации Java - PullRequest
0 голосов
/ 28 сентября 2018

Следующие функции используются для преобразования шестнадцатеричных и двоичных строк в их числовую форму.Я не уверен, как реализовать третий метод (hexCharDecode) в методе hexStringDecode.Я надеялся, что кто-нибудь может помочь объяснить, как я могу включить это и как это более эффективно, чем метод, который я уже определил для hexStringDecode ...?

Спасибо, милые люди!

 //method to identify and filter out the "0x" if it is provided using an if-else and to decode the hexadecimal

public static long hexStringDecode(String hex) {
    if (hex.charAt(0) == '0' && hex.charAt(1) == 'x') {
        return Long.parseLong(hex.substring(2), 16);
    } else {
        return Long.parseLong(hex, 16);
    }
}

public static short hexCharDecode(char digit) {
    String s = digit + "";
    return Short.parseShort(s, 16);
}

    //method to identify and filter out the "0b" if it is provided using an if-else and to decode the binary

public static short binaryStringDecode(String binary) {
    if (binary.charAt(0) == '0' && binary.charAt(1) == 'b') {
        return Short.parseShort(binary.substring(2), 2);
    } else {
        return Short.parseShort(binary, 2);
    }
}

    //method to convert binary to hex
public static String binaryToHex(String binary) {
    short s = binaryStringDecode(binary);
    return Integer.toHexString(s).toUpperCase();
}

сильный текст

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...