Search code examples
javastringdouble

How to convert from a double without a fractional part to a hexaicosadecimal string (base-26)?


I am wondering how do you convert from a double without a fractional part to a hexaicosadecimal string? What are some methods for converting and are there short cuts?


Solution

  • As far as I understood it is a base 26 conversion to letters only, with a prefix "c". Not sure on the sign for a negative- number.

    One "shortcut" would be to take the standard base 26 conversion. Arguably doing the conversion oneself is just as easy, and would certainly give a better note in homework.

    static String hexaicosadecimal(double x) {
        long n = (long)x;
        String base26 = Long.toString(n, 26); // 0..9a.p
        return "c" + base26.codePoints()
            .map(cp -> '0' <= cp && cp <= '9' ? 'A' + (cp - '0')
                            : 'a' <= cp && cp < 'a' + 16 ? 'A' + 10 + (cp - 'a')
                            : cp) // '-'
            .collect(StringBuilder::new,
                StringBuilder::appendCodePoint,
                StringBuilder::append)
            .toString();
    }