hex String To Byte array - Java java.lang

Java examples for java.lang:byte Array to String

Description

hex String To Byte array

Demo Code


//package com.java2s;

public class Main {


    public static byte[] hexToBytes(String str) {
        if (str == null) {
            return null;
        } else if (str.length() < 2) {
            return null;
        } else {/*from  w  w w  .j a v a 2 s. c o  m*/
            int len = str.length() / 2;
            byte[] buffer = new byte[len];
            for (int i = 0; i < len; i++) {
                buffer[i] = (byte) Integer.parseInt(
                        str.substring(i * 2, i * 2 + 2), 16);
            }
            return buffer;
        }
    }
}

Related Tutorials