Java Byte Create toByteByHex(String address)

Here you can find the source of toByteByHex(String address)

Description

to Byte By Hex

License

Apache License

Declaration

public static byte[] toByteByHex(String address) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {

    public static byte[] toByteByHex(String address) {
        if (address == null) {
            throw new IllegalArgumentException("address is invalid");
        }//from  w w  w  .  j  a v a  2 s  .  co  m
        int len = address.length();
        if (len < 8) {
            throw new IllegalArgumentException("address is invalid");
        }
        byte[] buf;
        if (len >= 12) {
            buf = new byte[6];
        } else {
            buf = new byte[4];
        }
        int pos = 0;
        if (len >= 12) {
            buf[pos++] = (byte) Integer.parseInt(address.substring(10, 12), 16);
            buf[pos++] = (byte) Integer.parseInt(address.substring(8, 10), 16);
        }
        buf[pos++] = (byte) Integer.parseInt(address.substring(0, 2), 16);
        buf[pos++] = (byte) Integer.parseInt(address.substring(2, 4), 16);
        buf[pos++] = (byte) Integer.parseInt(address.substring(4, 6), 16);
        buf[pos++] = (byte) Integer.parseInt(address.substring(6, 8), 16);
        return buf;
    }
}

Related

  1. toByte(String value)
  2. toByte(String value)
  3. toByte(T value)
  4. toByteBE(int value, byte[] dst, int offset)
  5. toByteByAddress(String address)
  6. toByteColor(int i)
  7. toByteFromBin(String binSymbols)
  8. toByteMatrix(Number[][] matrix)
  9. toByteObject(String value, Byte defaultValue)