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

  • SEARCH
  • Home
  • 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 601091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:40:37+00:00 2026-05-13T16:40:37+00:00

I’m currently looking at a simple programming problem that might be fun to optimize

  • 0

I’m currently looking at a simple programming problem that might be fun to optimize – at least for anybody who believes that programming is art 🙂 So here is it:

How to best represent long’s as Strings while keeping their natural order?

Additionally, the String representation should match ^[A-Za-z0-9]+$. (I’m not too strict here, but avoid using control characters or anything that might cause headaches with encodings, is illegal in XML, has line breaks, or similar characters that will certainly cause problems)

Here’s a JUnit test case:

@Test
public void longConversion() {
    final long[] longs = { Long.MIN_VALUE, Long.MAX_VALUE, -5664572164553633853L,
            -8089688774612278460L, 7275969614015446693L, 6698053890185294393L,
            734107703014507538L, -350843201400906614L, -4760869192643699168L,
            -2113787362183747885L, -5933876587372268970L, -7214749093842310327L, };

    // keep it reproducible
    //Collections.shuffle(Arrays.asList(longs));

    final String[] strings = new String[longs.length];
    for (int i = 0; i < longs.length; i++) {
        strings[i] = Converter.convertLong(longs[i]);
    }

    // Note: Comparator is not an option
    Arrays.sort(longs);
    Arrays.sort(strings);

    final Pattern allowed = Pattern.compile("^[A-Za-z0-9]+$");
    for (int i = 0; i < longs.length; i++) {
        assertTrue("string: " + strings[i], allowed.matcher(strings[i]).matches());
        assertEquals("string: " + strings[i], longs[i], Converter.parseLong(strings[i]));
    }
}

and here are the methods I’m looking for

public static class Converter {
    public static String convertLong(final long value) {
        // TODO
    }

    public static long parseLong(final String value) {
        // TODO
    }
}

I already have some ideas on how to approach this problem. Still, I though I might get some nice (creative) suggestions from the community.

Additionally, it would be nice if this conversion would be

  • as short as possible
  • easy to implement in other languages

EDIT: I’m quite glad to see that two very reputable programmers ran into the same problem as I did: using ‘-‘ for negative numbers can’t work as the ‘-‘ doesn’t reverse the order of sorting:

  1. -0001
  2. -0002
  3. 0000
  4. 0001
  5. 0002
  • 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-13T16:40:37+00:00Added an answer on May 13, 2026 at 4:40 pm

    Ok, take two:

    class Converter {
      public static String convertLong(final long value) {
        return String.format("%016x", value - Long.MIN_VALUE);
      }
    
      public static long parseLong(final String value) {
        String first = value.substring(0, 8);
        String second = value.substring(8);
        long temp = (Long.parseLong(first, 16) << 32) | Long.parseLong(second, 16);
        return temp + Long.MIN_VALUE;
      }
    }
    

    This one takes a little explanation. Firstly, let me demonstrate that it is reversible and the resultant conversions should demonstrate the ordering:

    for (long aLong : longs) {
      String out = Converter.convertLong(aLong);
      System.out.printf("%20d %16s %20d\n", aLong, out, Converter.parseLong(out));
    }
    

    Output:

    -9223372036854775808 0000000000000000 -9223372036854775808
     9223372036854775807 ffffffffffffffff  9223372036854775807
    -5664572164553633853 316365a0e7370fc3 -5664572164553633853
    -8089688774612278460 0fbba6eba5c52344 -8089688774612278460
     7275969614015446693 e4f96fd06fed3ea5  7275969614015446693
     6698053890185294393 dcf444867aeaf239  6698053890185294393
      734107703014507538 8a301311010ec412   734107703014507538
     -350843201400906614 7b218df798a35c8a  -350843201400906614
    -4760869192643699168 3dedfeb1865f1e20 -4760869192643699168
    -2113787362183747885 62aa5197ea53e6d3 -2113787362183747885
    -5933876587372268970 2da6a2aeccab3256 -5933876587372268970
    -7214749093842310327 1be00fecadf52b49 -7214749093842310327
    

    As you can see Long.MIN_VALUE and Long.MAX_VALUE (the first two rows) are correct and the other values basically fall in line.

    What is this doing?

    Assuming signed byte values you have:

    • -128 => 0x80
    • -1 => 0xFF
    • 0 => 0x00
    • 1 => 0x01
    • 127 => 0x7F

    Now if you add 0x80 to those values you get:

    • -128 => 0x00
    • -1 => 0x7F
    • 0 => 0x80
    • 1 => 0x81
    • 127 => 0xFF

    which is the correct order (with overflow).

    Basically the above is doing that with 64 bit signed longs instead of 8 bit signed bytes.

    The conversion back is a little more roundabout. You might think you can use:

    return Long.parseLong(value, 16);
    

    but you can’t. Pass in 16 f’s to that function (-1) and it will throw an exception. It seems to be treating that as an unsigned hex value, which long cannot accommodate. So instead I split it in half and parse each piece, combining them together, left-shifting the first half by 32 bits.

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

Sidebar

Ask A Question

Stats

  • Questions 438k
  • Answers 438k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can't specify the order, or any other option for… May 15, 2026 at 4:31 pm
  • Editorial Team
    Editorial Team added an answer If I was going to be calling it via requestAction… May 15, 2026 at 4:31 pm
  • Editorial Team
    Editorial Team added an answer In DDMS there is a tab named "Emulator Control" you… May 15, 2026 at 4:31 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.