Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import android.annotation.SuppressLint;

public class Main {

    @SuppressLint("DefaultLocale")
    public static byte[] getBytesFromHexString(String hexstring) {
        if (hexstring == null || hexstring.equals("")) {
            return null;
        }
        hexstring = hexstring.replace(" ", "");
        hexstring = hexstring.toUpperCase();
        int size = hexstring.length() / 2;
        char[] hexarray = hexstring.toCharArray();
        byte[] rv = new byte[size];
        for (int i = 0; i < size; i++) {
            int pos = i * 2;
            rv[i] = (byte) (charToByte(hexarray[pos]) << 4 | charToByte(hexarray[pos + 1]));
        }
        return rv;
    }

    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
}