org.apache.commons.codec.binary.Hex.decode()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(7.4k)|赞(0)|评价(0)|浏览(229)

本文整理了Java中org.apache.commons.codec.binary.Hex.decode()方法的一些代码示例,展示了Hex.decode()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Hex.decode()方法的具体详情如下:
包路径:org.apache.commons.codec.binary.Hex
类名称:Hex
方法名:decode

Hex.decode介绍

[英]Converts a String or an array of character bytes representing hexidecimal values into an array of bytes of those same values. The returned array will be half the length of the passed String or array, as it takes two characters to represent any given byte. An exception is thrown if the passed char array has an odd number of elements.
[中]将表示十六进制值的字符串或字符字节数组转换为具有相同值的字节数组。返回的数组将是传递的字符串或数组长度的一半,因为它需要两个字符来表示任何给定的字节。如果传递的char数组具有奇数个元素,则引发异常。

代码示例

代码示例来源:origin: commons-codec/commons-codec

/**
 * Converts a String or an array of character bytes representing hexadecimal values into an array of bytes of those
 * same values. The returned array will be half the length of the passed String or array, as it takes two characters
 * to represent any given byte. An exception is thrown if the passed char array has an odd number of elements.
 *
 * @param object
 *            A String, ByteBuffer, byte[], or an array of character bytes containing hexadecimal digits
 * @return A byte array containing binary data decoded from the supplied byte array (representing characters).
 * @throws DecoderException
 *             Thrown if an odd number of characters is supplied to this function or the object is not a String or
 *             char[]
 * @see #decodeHex(char[])
 */
@Override
public Object decode(final Object object) throws DecoderException {
  if (object instanceof String) {
    return decode(((String) object).toCharArray());
  } else if (object instanceof byte[]) {
    return decode((byte[]) object);
  } else if (object instanceof ByteBuffer) {
    return decode((ByteBuffer) object);
  } else {
    try {
      return decodeHex((char[]) object);
    } catch (final ClassCastException e) {
      throw new DecoderException(e.getMessage(), e);
    }
  }
}

代码示例来源:origin: apache/nifi

@Override
  public void process(InputStream in, OutputStream out) throws IOException {
    int len;
    byte[] inBuf = new byte[8192];
    Hex h = new Hex();
    while ((len = in.read(inBuf)) > 0) {
      // If the input buffer is of odd length, try to get another byte
      if (len % 2 != 0) {
        int b = in.read();
        if (b != -1) {
          inBuf[len] = (byte) b;
          len++;
        }
      }
      // Construct a new buffer bounded to len
      byte[] slice = Arrays.copyOfRange(inBuf, 0, len);
      try {
        out.write(h.decode(slice));
      } catch (DecoderException ex) {
        throw new IOException(ex);
      }
    }
    out.flush();
  }
}

代码示例来源:origin: commons-codec/commons-codec

@Test
public void testEncodeDecodeHexCharArrayRandom() throws DecoderException, EncoderException {
  final Random random = new Random();
  final Hex hex = new Hex();
  for (int i = 5; i > 0; i--) {
    final byte[] data = new byte[random.nextInt(10000) + 1];
    random.nextBytes(data);
    // static API
    final char[] encodedChars = Hex.encodeHex(data);
    byte[] decodedBytes = Hex.decodeHex(encodedChars);
    assertTrue(Arrays.equals(data, decodedBytes));
    // instance API with array parameter
    final byte[] encodedStringBytes = hex.encode(data);
    decodedBytes = hex.decode(encodedStringBytes);
    assertTrue(Arrays.equals(data, decodedBytes));
    // instance API with char[] (Object) parameter
    String dataString = new String(encodedChars);
    char[] encodedStringChars = (char[]) hex.encode(dataString);
    decodedBytes = (byte[]) hex.decode(encodedStringChars);
    assertTrue(Arrays.equals(StringUtils.getBytesUtf8(dataString), decodedBytes));
    // instance API with String (Object) parameter
    dataString = new String(encodedChars);
    encodedStringChars = (char[]) hex.encode(dataString);
    decodedBytes = (byte[]) hex.decode(new String(encodedStringChars));
    assertTrue(Arrays.equals(StringUtils.getBytesUtf8(dataString), decodedBytes));
  }
}

代码示例来源:origin: commons-codec/commons-codec

@Test
public void testDecodeByteBufferOddCharacters() {
  final ByteBuffer buffer = ByteBuffer.allocate(1);
  buffer.put((byte) 65);
  try {
    new Hex().decode(buffer);
    fail("An exception wasn't thrown when trying to decode an odd number of characters");
  } catch (final DecoderException e) {
    // Expected exception
  }
}

代码示例来源:origin: commons-codec/commons-codec

@Test
public void testDecodeByteBufferEmpty() throws DecoderException {
  assertTrue(Arrays.equals(new byte[0], new Hex().decode(ByteBuffer.allocate(0))));
}

代码示例来源:origin: commons-codec/commons-codec

@Test
public void testDecodeByteBufferObjectEmpty() throws DecoderException {
  assertTrue(Arrays.equals(new byte[0], (byte[]) new Hex().decode((Object) ByteBuffer.allocate(0))));
}

代码示例来源:origin: commons-codec/commons-codec

@Test
public void testDecodeByteArrayObjectEmpty() throws DecoderException {
  assertTrue(Arrays.equals(new byte[0], (byte[]) new Hex().decode((Object) new byte[0])));
}

代码示例来源:origin: commons-codec/commons-codec

@Test
public void testDecodeStringEmpty() throws DecoderException {
  assertTrue(Arrays.equals(new byte[0], (byte[]) new Hex().decode("")));
}

代码示例来源:origin: commons-codec/commons-codec

@Test
public void testDecodeByteArrayEmpty() throws DecoderException {
  assertTrue(Arrays.equals(new byte[0], new Hex().decode(new byte[0])));
}

代码示例来源:origin: commons-codec/commons-codec

@Test
public void testDecodeBadCharacterPos1() {
  try {
    new Hex().decode("0q");
    fail("An exception wasn't thrown when trying to decode an illegal character");
  } catch (final DecoderException e) {
    // Expected exception
  }
}

代码示例来源:origin: commons-codec/commons-codec

@Test
public void testDecodeBadCharacterPos0() {
  try {
    new Hex().decode("q0");
    fail("An exception wasn't thrown when trying to decode an illegal character");
  } catch (final DecoderException e) {
    // Expected exception
  }
}

代码示例来源:origin: commons-codec/commons-codec

@Test
public void testDecodeClassCastException() {
  try {
    new Hex().decode(new int[] { 65 });
    fail("An exception wasn't thrown when trying to decode.");
  } catch (final DecoderException e) {
    // Expected exception
  }
}

代码示例来源:origin: commons-codec/commons-codec

@Test
public void testDecodeHexStringOddCharacters() {
  try {
    new Hex().decode("6");
    fail("An exception wasn't thrown when trying to decode an odd number of characters");
  } catch (final DecoderException e) {
    // Expected exception
  }
}

代码示例来源:origin: commons-codec/commons-codec

@Test
public void testDecodeByteArrayOddCharacters() {
  try {
    new Hex().decode(new byte[] { 65 });
    fail("An exception wasn't thrown when trying to decode an odd number of characters");
  } catch (final DecoderException e) {
    // Expected exception
  }
}

代码示例来源:origin: commons-codec/commons-codec

final byte[] decodedUtf8Bytes = (byte[]) utf8Codec.decode(expectedHexString);
actualStringFromBytes = new String(decodedUtf8Bytes, utf8Codec.getCharset());
final byte[] decodedCustomBytes = customCodec.decode(actualEncodedBytes);
actualStringFromBytes = new String(decodedCustomBytes, name);
assertEquals(name, sourceString, actualStringFromBytes);

代码示例来源:origin: apache/accumulo

Hex hex = new Hex();
ByteArrayInputStream bais = new ByteArrayInputStream(
  hex.decode(tokens[1].split("=")[1].getBytes(UTF_8)));
DataInputStream dis = new DataInputStream(bais);

代码示例来源:origin: NemProject/nem.core

private static byte[] getBytesInternal(final String hexString) throws DecoderException {
  final Hex codec = new Hex();
  final String paddedHexString = 0 == hexString.length() % 2 ? hexString : "0" + hexString;
  final byte[] encodedBytes = StringEncoder.getBytes(paddedHexString);
  return codec.decode(encodedBytes);
}

代码示例来源:origin: nemtech/nem2-sdk-java

private static byte[] getBytesInternal(final String hexString) throws DecoderException {
  final Hex codec = new Hex();
  final String paddedHexString = 0 == hexString.length() % 2 ? hexString : "0" + hexString;
  final byte[] encodedBytes = StringEncoder.getBytes(paddedHexString);
  return codec.decode(encodedBytes);
}

代码示例来源:origin: badoualy/kotlogram

public static byte[] load(File file) throws IOException, DecoderException {
  Charset charset = Charset.forName("UTF-8");
  return (byte[]) new Hex(charset).decode(FileUtils.readFileToString(file, charset));
}

代码示例来源:origin: com.threewks.thundr/thundr

public Encoder unhex() {
  try {
    data = new Hex().decode(data);
    return this;
  } catch (DecoderException e) {
    throw new BaseException(e, "Failed to convert data from hex: %s", e.getMessage());
  }
}

相关文章