Java Code Examples for com.helger.commons.string.StringHelper#getHexValue()

The following examples show how to use com.helger.commons.string.StringHelper#getHexValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonEscapeHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
private static int _hexval (final char c)
{
  final int ret = StringHelper.getHexValue (c);
  if (ret < 0)
    throw new IllegalArgumentException ("Illegal hex char '" + c + "'");
  return ret;
}
 
Example 2
Source File: ASCIIHexCodec.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
public void decode (@Nullable final byte [] aEncodedBuffer,
                    @Nonnegative final int nOfs,
                    @Nonnegative final int nLen,
                    @Nonnull @WillNotClose final OutputStream aOS)
{
  if (aEncodedBuffer == null || nLen == 0)
    return;

  try
  {
    boolean bFirstByte = true;
    int nFirstByte = 0;
    for (int i = 0; i < nLen; ++i)
    {
      final byte nEncByte = aEncodedBuffer[nOfs + i];
      if (nEncByte == '>')
        break;

      // Ignore whitespaces
      if (Character.isWhitespace (nEncByte))
        continue;

      final byte nDecByte = (byte) StringHelper.getHexValue ((char) nEncByte);
      if (nDecByte == CGlobal.ILLEGAL_UINT)
        throw new DecodeException ("Failed to convert byte '" +
                                   nEncByte +
                                   "/" +
                                   ((char) nEncByte) +
                                   "' to hex value in ASCIIHexDecode");
      if (bFirstByte)
        nFirstByte = nDecByte;
      else
        aOS.write ((byte) (nFirstByte << 4 | nDecByte & 0xff));
      bFirstByte = !bFirstByte;
    }

    // Write trailing byte
    if (!bFirstByte)
      aOS.write ((byte) (nFirstByte << 4));
  }
  catch (final IOException ex)
  {
    throw new DecodeException ("Failed to decode ASCII Hex", ex);
  }
}
 
Example 3
Source File: JsonParser.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * @param aStartPos
 *        Optional parsing start position
 * @param c
 *        character
 * @return The int representation of the read hex char (0-9, a-f)
 * @throws JsonParseException
 *         In case a non hex char was encountered
 */
private int _getHexValue (@Nullable final IJsonParsePosition aStartPos, final int c) throws JsonParseException
{
  final int ret = StringHelper.getHexValue ((char) c);
  if (ret == -1)
    throw _parseEx (aStartPos, "Invalid hex character " + _getPrintableChar (c) + " provided!");
  return ret;
}