-7

The function translates a string containing the decimal representation of an integer into a string containing its hexadecimal representation. Thanks in advance for the code)

New contributor
vladislav is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

1 Answer 1

-2

Questions are supposed to show what its author has tried before. But since i was interested in this myself, i just wrote it myself.

I used Java 11 and the Formatter documentation.

    private static String convertDecimalStringToHexString(String inputString) {
        Integer inputInteger = Integer.parseInt(inputString);
        return String.format("%X", inputInteger);
    }

Test code

    public static void main(String[] args) {
        String[] inputStrings = new String[] { "31", "16777215", "16777216" };
        for (String inputString : inputStrings) {
            System.out.println(convertDecimalStringToHexString(inputString));
        }
    }

Test output

1F
FFFFFF
1000000

Not the answer you're looking for? Browse other questions tagged or ask your own question.