Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6578463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:46:27+00:00 2026-05-25T15:46:27+00:00

I wrote an utility class to encode numbers in a custom numeral system with

  • 0

I wrote an utility class to encode numbers in a custom numeral system with base N. As any self-respecting Java programmer I then wrote a unit test to check that the code works as expected (for any number I could throw at it).

It turned out, that for small numbers, it worked. However, for sufficiently large numbers, the tests failed.

The code:

public class EncodeUtil {

    private String symbols;

    private boolean isCaseSensitive;
    private boolean useDefaultSymbols;

    private int[] symbolLookup = new int[255];

    public EncodeUtil() {
        this(true);
    }

    public EncodeUtil(boolean isCaseSensitive) {
        this.useDefaultSymbols = true;
        setCaseSensitive(isCaseSensitive);
    }

    public EncodeUtil(boolean isCaseSensitive, String symbols) {
        this.useDefaultSymbols = false;
        setCaseSensitive(isCaseSensitive);
        setSymbols(symbols);
    }

    public void setSymbols(String symbols) {
        this.symbols = symbols;
        fillLookupArray();
    }

    public void setCaseSensitive(boolean isCaseSensitive) {
        this.isCaseSensitive = isCaseSensitive;
        if (useDefaultSymbols) {
            setSymbols(makeAlphaNumericString(isCaseSensitive));
        }
    }

    private void fillLookupArray() {
        //reset lookup array
        for (int i = 0; i < symbolLookup.length; i++) {
            symbolLookup[i] = -1;
        }
        for (int i = 0; i < symbols.length(); i++) {
            char c = symbols.charAt(i);
            if (symbolLookup[(int) c] == -1) {
                symbolLookup[(int) c] = i;
            } else {
                throw new IllegalArgumentException("duplicate symbol:" + c);
            }
        }
    }

    private static String makeAlphaNumericString(boolean caseSensitive) {
        StringBuilder sb = new StringBuilder(255);
        int caseDiff = 'a' - 'A';
        for (int i = 'A'; i <= 'Z'; i++) {
            sb.append((char) i);
            if (caseSensitive) sb.append((char) (i + caseDiff));
        }
        for (int i = '0'; i <= '9'; i++) {
            sb.append((char) i);
        }
        return sb.toString();
    }

    public String encodeNumber(long decNum) {
        return encodeNumber(decNum, 0);
    }

    public String encodeNumber(long decNum, int minLen) {
        StringBuilder result = new StringBuilder(20);
        long num = decNum;
        long mod = 0;
        int base = symbols.length();
        do {
            mod = num % base;
            result.append(symbols.charAt((int) mod));
            num = Math.round(Math.floor((num-mod) / base));
        } while (num > 0);
        if (result.length() < minLen) {
            for (int i = result.length(); i < minLen; i++) {
                result.append(symbols.charAt(0));
            }
        }
        return result.toString();
    }

    public long decodeNumber(String encNum) {
        if (encNum == null) return 0;
        if (!isCaseSensitive) encNum = encNum.toUpperCase();
        long result = 0;
        int base = symbols.length();
        long multiplier = 1;
        for (int i = 0; i < encNum.length(); i++) {
            char c = encNum.charAt(i);
            int pos = symbolLookup[(int) c];
            if (pos == -1) {
                String debugValue = encNum.substring(0, i) + "[" + c + "]";
                if (encNum.length()-1 > i) {
                    debugValue += encNum.substring(i + 1);
                }
                throw new IllegalArgumentException(
                    "invalid symbol '" + c + "' at position " 
                    + (i+1) + ": " + debugValue);
            } else {
                result += pos * multiplier;
                multiplier = multiplier * base;
            }
        }
        return result;
    }

    @Override
    public String toString() {
        return symbols;
    }

}

The test:

public class EncodeUtilTest {

    @Test
    public void testRoundTrip() throws Exception {
        //for some reason, numbers larger than this range will not be decoded correctly
        //maybe some bug in JVM with arithmetic with long values?
        //tried also BigDecimal, didn't make any difference
        //anyway, it is highly improbable that we ever need such large numbers
        long value = 288230376151711743L;
        test(value, new EncodeUtil());
        test(value, new EncodeUtil(false));
        test(value, new EncodeUtil(true, "1234567890qwertyuiopasdfghjklzxcvbnm"));
    }

    @Test
    public void testRoundTripMax() throws Exception {
        //this will fail, see above
        test(Long.MAX_VALUE, new EncodeUtil());
    }

    @Test
    public void testRoundTripGettingCloserToMax() throws Exception {
        //here we test different values, getting closer to Long.MAX_VALUE
        //this will fail, see above
        EncodeUtil util = new EncodeUtil();
        for (long i = 1000; i > 0; i--) {
            System.out.println(i);
            test(Long.MAX_VALUE / i, util);
        }
    }

    private void test(long number, EncodeUtil util) throws Exception {
        String encoded = util.encodeNumber(number);
        long result = util.decodeNumber(encoded);
        long diff = number - result;
        //System.out.println(number + " = " + encoded + " diff " + diff);
        assertEquals("original=" + number + ", result=" + result + ", encoded=" + encoded, 0, diff);
    }

}

Any ideas why things start failing when the values get large? I also tried BigInteger, but it did not seem to make a difference.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:46:28+00:00Added an answer on May 25, 2026 at 3:46 pm

    You’re using floating point maths in your encodeNumber method, which makes your code rely on the precision of the double type.

    Replacing

    num = Math.round(Math.floor((num-mod) / base));
    

    with

    num = (num - mod) / base;
    

    Makes the tests pass. Actually

    num = num / base;
    

    Should work just as well (thought experiment: what is 19 / 10 when / is integer division?).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My main solution is using code from a utility class library, that I wrote
I just wrote some new utility methods within a non-page class for an existing
I wrote a little utility class that saves BitmapSource objects to image files. The
Is there a.NET utility class equivalent to java.util.Arrays.hashCode() for arrays of intrinsic types such
i write some utility class but how to get name ? Can that send
In C# with MVC, i want to write a common utility or class in
I wrote a utility for photographers that I plan to sell online pretty cheap
I wrote a disk utility that allowed you to erase whole physical drives. it
In my game I have a base class of Loot which has methods universal
I wrote this utility function: public static <T> List<T> pluck(String fieldName, List list) throws

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.