public String encode(String s)
  {
    if (s == null)
    {
      return null;
    }
    StringBuilder sb = new StringBuilder();
    int len = s.length();
    for (int i = 0; i < len; i++)
    {
      char c = s.charAt(i);
      if (c == _encodingChar || _reservedChars.contains(c))
      {
        sb.append(_encodingChar);
        int asciiVal = (int) c;
        if (asciiVal < 16) // <= 'F'
        {
          sb.append('0');
        }
        sb.append(Integer.toHexString(asciiVal).toUpperCase());
      }
      else
      {
        sb.append(c);
      }
    }

    return sb.toString();
  }
@Test
  public void testSingleCharEncode()
  {
    // test chars we care about plus the corner cases
    Assert.assertEquals(TEST_ENCODING_1.encode("."), "~2E");
    Assert.assertEquals(TEST_ENCODING_1.encode("["), "~5B");
    Assert.assertEquals(TEST_ENCODING_1.encode("]"), "~5D");
    Assert.assertEquals(TEST_ENCODING_1.encode("~"), "~7E");
    Assert.assertEquals(TEST_ENCODING_1.encode("\020"), "~10"); // first char that does not require 0 padding
    Assert.assertEquals(TEST_ENCODING_1.encode("\017"), "~0F"); // last char that requires 0 padding
    Assert.assertEquals(TEST_ENCODING_1.encode("\000"), "~00"); // first char
    Assert.assertEquals(TEST_ENCODING_1.encode("\177"), "~7F"); // last char
  }