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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:38:59+00:00 2026-05-13T06:38:59+00:00

I had a requirement of encoding a 3 character string(always alphabets) into a 2

  • 0

I had a requirement of encoding a 3 character string(always alphabets) into a 2 byte[] array of 2 integers.
This was to be done to save space and performance reasons.

Now the requirement has changed a bit. The String will be of variable length. It will either be of length 3 (as it is above) or will be of length 4 and will have 1 special character at beginning. The special character is fixed i.e. if we choose @ it will always be @ and always at the beginning. So we are sure that if length of String is 3, it will have only alphabets and if length is 4, the first character will always be ‘@’ followed by 3 alphabets

So I can use

charsAsNumbers[0] = (byte) (locationChars[0] - '@');

instead of

charsAsNumbers[0] = (byte) (chars[0] - 'A');

Can I still encode the 3 or 4 chars to 2 byte array and decode them back? If so, 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-13T06:38:59+00:00Added an answer on May 13, 2026 at 6:38 am

    Yes, it is possible to encode an extra bit of information while maintaining the previous encoding for 3 character values. But since your original encoding doesn’t leave nice clean swaths of free numbers in the output set, mapping of the additional set of Strings introduced by adding that extra character cannot help but be a little discontinuous.

    Accordingly, I think it would be hard to come up with mapping functions that handle these discontinuities without being both awkward and slow. I conclude that a table-based mapping is the only sane solution.

    I was too lazy to re-engineer your mapping code, so I incorporated it into the table initialization code of mine; this also eliminates many opportunities for translation errors 🙂 Your encode() method is what I call OldEncoder.encode().

    I’ve run a small test program to verify that NewEncoder.encode() comes up with the same values as OldEncoder.encode(), and is in addition able to encode Strings with a leading 4th character. NewEncoder.encode() doesn’t care what the character is, it goes by String length; for decode(), the character used can be defined using PREFIX_CHAR . I’ve also eyeball checked that the byte array values for prefixed Strings don’t duplicate any of those for non-prefixed Strings; and finally, that encoded prefixed Strings can indeed be converted back to the same prefixed Strings.

    package tequilaguy;
    
    
    public class NewConverter {
    
       private static final String[] b2s = new String[0x10000];
       private static final int[] s2b = new int[0x10000];
       static { 
          createb2s();
          creates2b();
       }
    
       /**
        * Create the "byte to string" conversion table.
        */
       private static void createb2s() {
          // Fill 17576 elements of the array with b -> s equivalents.
          // index is the combined byte value of the old encode fn; 
          // value is the String (3 chars). 
          for (char a='A'; a<='Z'; a++) {
             for (char b='A'; b<='Z'; b++) {
                for (char c='A'; c<='Z'; c++) {
                   String str = new String(new char[] { a, b, c});
                   byte[] enc = OldConverter.encode(str);
                   int index = ((enc[0] & 0xFF) << 8) | (enc[1] & 0xFF);
                   b2s[index] = str;
                   // int value = 676 * a + 26 * b + c - ((676 + 26 + 1) * 'A'); // 45695;
                   // System.out.format("%s : %02X%02X = %04x / %04x %n", str, enc[0], enc[1], index, value);
                }
             }
          }
          // Fill 17576 elements of the array with b -> @s equivalents.
          // index is the next free (= not null) array index;
          // value = the String (@ + 3 chars)
          int freep = 0;
          for (char a='A'; a<='Z'; a++) {
             for (char b='A'; b<='Z'; b++) {
                for (char c='A'; c<='Z'; c++) {
                   String str = "@" + new String(new char[] { a, b, c});
                   while (b2s[freep] != null) freep++;
                   b2s[freep] = str;
                   // int value = 676 * a + 26 * b + c - ((676 + 26 + 1) * 'A') + (26 * 26 * 26);
                   // System.out.format("%s : %02X%02X = %04x / %04x %n", str, 0, 0, freep, value);
                }
             }
          }
       }
    
       /**
        * Create the "string to byte" conversion table.
        * Done by inverting the "byte to string" table.
        */
       private static void creates2b() {
          for (int b=0; b<0x10000; b++) {
             String s = b2s[b];
             if (s != null) {
                int sval;
                if (s.length() == 3) {
                   sval = 676 * s.charAt(0) + 26 * s.charAt(1) + s.charAt(2) - ((676 + 26 + 1) * 'A');
                } else {
                   sval = 676 * s.charAt(1) + 26 * s.charAt(2) + s.charAt(3) - ((676 + 26 + 1) * 'A') + (26 * 26 * 26);
                }
                s2b[sval] = b;
             }
          }
       }
    
       public static byte[] encode(String str) {
          int sval;
          if (str.length() == 3) {
             sval = 676 * str.charAt(0) + 26 * str.charAt(1) + str.charAt(2) - ((676 + 26 + 1) * 'A');
          } else {
             sval = 676 * str.charAt(1) + 26 * str.charAt(2) + str.charAt(3) - ((676 + 26 + 1) * 'A') + (26 * 26 * 26);
          }
          int bval = s2b[sval];
          return new byte[] { (byte) (bval >> 8), (byte) (bval & 0xFF) };
       }
    
       public static String decode(byte[] b) {
          int bval = ((b[0] & 0xFF) << 8) | (b[1] & 0xFF);
          return b2s[bval];
       }
    
    }
    

    I’ve left a few intricate constant expressions in the code, especially the powers-of-26 stuff. The code looks horribly mysterious otherwise. You can leave those as they are without losing performance, as the compiler folds them up like Kleenexes.


    Update:

    As the horror of X-mas approaches, I’ll be on the road for a while. I hope you’ll find this answer and code in time to make good use of it. In support of which effort I’ll throw in my little test program. It doesn’t directly check stuff but prints out the results of conversions in all significant ways and allows you to check them by eye and hand. I fiddled with my code (small tweaks once I got the basic idea down) until everything looked OK there. You may want to test more mechanically and exhaustively.

    package tequilaguy;
    
    public class ConverterHarness {
    
    //   private static void runOldEncoder() {
    //      for (char a='A'; a<='Z'; a++) {
    //         for (char b='A'; b<='Z'; b++) {
    //            for (char c='A'; c<='Z'; c++) {
    //               String str = new String(new char[] { a, b, c});
    //               byte[] enc = OldConverter.encode(str);
    //               System.out.format("%s : %02X%02X%n", str, enc[0], enc[1]);
    //            }
    //         }
    //      }
    //   }
    
       private static void testNewConverter() {
          for (char a='A'; a<='Z'; a++) {
             for (char b='A'; b<='Z'; b++) {
                for (char c='A'; c<='Z'; c++) {
                   String str = new String(new char[] { a, b, c});
                   byte[] oldEnc = OldConverter.encode(str);
                   byte[] newEnc = NewConverter.encode(str);
                   byte[] newEnc2 = NewConverter.encode("@" + str);
                   System.out.format("%s : %02X%02X %02X%02X %02X%02X %s %s %n", 
                         str, oldEnc[0], oldEnc[1], newEnc[0], newEnc[1], newEnc2[0], newEnc2[1],
                         NewConverter.decode(newEnc), NewConverter.decode(newEnc2));
                }
             }
          }
       }
       public static void main(String[] args) {
          testNewConverter();
       }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Assume the CSV data is in "something.csv" and delimited by… May 13, 2026 at 8:37 pm
  • Editorial Team
    Editorial Team added an answer You have to create the file, then read the resource… May 13, 2026 at 8:37 pm
  • Editorial Team
    Editorial Team added an answer You can start by turning on integer warnings: intwarning('on') This… May 13, 2026 at 8:37 pm

Related Questions

I had a requirement where I need to clear off the select all checkbox
I had a requirement to copy all fields from one form to another similar
I had a requirement to create a query in SQL Server where the search condition
I am working on a database application in C#. I had a requirement to
I have a requirement to export a dataset as a CSV file. I have

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.