Convert Hex string to byte array using Integer.parseInt - Java java.lang

Java examples for java.lang:String Hex

Description

Convert Hex string to byte array using Integer.parseInt

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String hex = "dead";
        System.out.println(java.util.Arrays.toString(fromHex(hex)));
    }//  www .  jav a  2s  . c o  m

    public static byte[] fromHex(String hex) {
        byte[] bytes = new byte[hex.length() / 2];
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = (byte) Integer.parseInt(
                    hex.substring(2 * i, 2 * i + 2), 16);
        }
        return bytes;
    }
}

Related Tutorials