Java Convert via ByteBuffer ToByteArray(String hexString)

Here you can find the source of ToByteArray(String hexString)

Description

To Byte Array

License

Open Source License

Declaration

public static byte[] ToByteArray(String hexString) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.nio.ByteBuffer;

public class Main {
    public static byte[] ToByteArray(String hexString) {
        final String[] hexChunks = hexString.replace(" ", "").split("(?<=\\G..)");

        byte[] result = new byte[hexChunks.length];
        for (int i = 0; i < hexChunks.length; i++) {
            result[i] = (byte) Integer.parseInt(hexChunks[i], 16);
        }/*from ww  w  .j  a  v a2  s  . c  o m*/

        return result;
    }

    public static byte[] ToByteArray(int i) {
        return ByteBuffer.allocate(4).putInt(i).array();
    }
}

Related

  1. toByteArray(int[] intArray)
  2. toByteArray(long[] data)
  3. ToByteArray(long[] data)
  4. toByteArray(ReadableByteChannel channel)
  5. toByteArray(String bits)
  6. toByteArray(String value)
  7. toByteArray(UUID uniqueId)
  8. toByteArray2(String filename)
  9. toByteArray3(String filename)