Convert hex to byte - Android java.lang

Android examples for java.lang:Byte Array

Description

Convert hex to byte

Demo Code


//package com.java2s;

public class Main {

    private static byte[] hex2byte(String inputString) {
        if (inputString == null || inputString.length() < 2) {
            return new byte[0];
        }//  w w w .  ja  va2 s  .c o  m
        inputString = inputString.toLowerCase();
        int l = inputString.length() / 2;
        byte[] result = new byte[l];
        for (int i = 0; i < l; ++i) {
            String tmp = inputString.substring(2 * i, 2 * i + 2);
            result[i] = (byte) (Integer.parseInt(tmp, 16) & 0xFF);
        }
        return result;
    }
}

Related Tutorials