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 6559551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:20:33+00:00 2026-05-25T13:20:33+00:00

I’m looking for a way to shorten an already short string as much as

  • 0

I’m looking for a way to shorten an already short string as much as possible.

The string is a hostname:port combo and could look like “my-domain.se:2121” or “123.211.80.4:2122“.

I know regular compression is pretty much out of the question on strings this short due to the overhead needed and the lack of repetition but I have an idea of how to do it.

Because the alphabet is limited to 39 characters ([a-z][0-9]-:.) every character could fit in 6 bits. This reduce the length with up to 25% compared to ASCII. So my suggestion is somthing along these lines:

  1. Encode the string to a byte array using some kind of custom encoding
  2. Decode the byte array to a UTF-8 or ASCII string (this string will obviously not make any sense).

And then reverse the process to get the original string.

So to my questions:

  1. Could this work?
  2. Is there a better way?
  3. How?
  • 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-25T13:20:33+00:00Added an answer on May 25, 2026 at 1:20 pm

    You could encode the string as base 40 which is more compact than base 64. This will give you 12 such tokens into a 64 bit long. The 40th token could be the end of string marker to give you the length (as it will not be a whole number of bytes any more)

    If you use arithmetic encoding, it could be much smaller but you would need a table of frequencies for each token. (using a long list of possible examples)

    class Encoder {
      public static final int BASE = 40;
      StringBuilder chars = new StringBuilder(BASE);
      byte[] index = new byte[256];
    
      {
        chars.append('\0');
        for (char ch = 'a'; ch <= 'z'; ch++) chars.append(ch);
        for (char ch = '0'; ch <= '9'; ch++) chars.append(ch);
        chars.append("-:.");
        Arrays.fill(index, (byte) -1);
        for (byte i = 0; i < chars.length(); i++)
          index[chars.charAt(i)] = i;
      }
    
      public byte[] encode(String address) {
        try {
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          DataOutputStream dos = new DataOutputStream(baos);
          for (int i = 0; i < address.length(); i += 3) {
            switch (Math.min(3, address.length() - i)) {
              case 1: // last one.
                byte b = index[address.charAt(i)];
                dos.writeByte(b);
                break;
    
              case 2:
                char ch = (char) ((index[address.charAt(i+1)]) * 40 + index[address.charAt(i)]);
                dos.writeChar(ch);
                break;
    
              case 3:
                char ch2 = (char) ((index[address.charAt(i+2)] * 40 + index[address.charAt(i + 1)]) * 40 + index[address.charAt(i)]);
                dos.writeChar(ch2);
                break;
            }
          }
          return baos.toByteArray();
        } catch (IOException e) {
          throw new AssertionError(e);
        }
      }
    
      public static void main(String[] args) {
        Encoder encoder = new Encoder();
        for (String s : "twitter.com:2122,123.211.80.4:2122,my-domain.se:2121,www.stackoverflow.com:80".split(",")) {
          System.out.println(s + " (" + s.length() + " chars) encoded is " + encoder.encode(s).length + " bytes.");
        }
      }
    }
    

    prints

    twitter.com:2122 (16 chars) encoded is 11 bytes.
    123.211.80.4:2122 (17 chars) encoded is 12 bytes.
    my-domain.se:2121 (17 chars) encoded is 12 bytes.
    www.stackoverflow.com:80 (24 chars) encoded is 16 bytes.
    

    I leave decoding as an exercise. 😉

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
Specifically, suppose I start with the string string =hello \'i am \' me And
I am trying to render a haml file in a javascript response like so:

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.