title: Unique hash with maximum of 4 characters?
body: <p>What is the best method to create a hash of <code>String</code>, if the hash may not have more than 4 characters, and those 4 characters may only be lowercase letters or digits?</p>
  
  <p>The strings I want to hash have 1-255 characters.
  I know that it's probably impossible to create as 4-char hash without collision. But it would be sufficient if I'd have a good hash where possible collisions are minimized.</p>
  
  <p>What I tried is the <code>CRC16CCITT</code> from here:
  <a href="http://introcs.cs.princeton.edu/java/61data/CRC16CCITT.java" rel="nofollow noreferrer">http://introcs.cs.princeton.edu/java/61data/CRC16CCITT.java</a></p>
  
  <pre><code>public class CRC16CCITT { 
  
      public static void main(String[] args) { 
          int crc = 0xFFFF;          // initial value
          int polynomial = 0x1021;   // 0001 0000 0010 0001  (0, 5, 12) 
  
          // byte[] testBytes = "123456789".getBytes("ASCII");
  
          byte[] bytes = args[0].getBytes();
  
          for (byte b : bytes) {
              for (int i = 0; i &lt; 8; i++) {
                  boolean bit = ((b   &gt;&gt; (7-i) &amp; 1) == 1);
                  boolean c15 = ((crc &gt;&gt; 15    &amp; 1) == 1);
                  crc &lt;&lt;= 1;
                  if (c15 ^ bit) crc ^= polynomial;
              }
          }
  
          crc &amp;= 0xffff;
          StdOut.println("CRC16-CCITT = " + Integer.toHexString(crc));
      }
  
  }
  </code></pre>
  
  <p>But this gives too many collision. Are there better algorithms?</p>