Java Byte Array Create toBytes4HexString(String str, char... separateds)

Here you can find the source of toBytes4HexString(String str, char... separateds)

Description

to Bytes Hex String

License

Apache License

Declaration

public static byte[] toBytes4HexString(String str, char... separateds) 

Method Source Code

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

public class Main {
    public static byte[] toBytes4HexString(String str, char... separateds) {
        if (str == null)
            return new byte[0];
        char separated = ' ';
        if (separateds != null && separateds.length == 1) {
            separated = separateds[0];/* w  w  w  .  j a v a2  s . co  m*/
            String[] strArr = str.split(separated + "");
            byte[] array = new byte[strArr.length];
            for (int i = 0; i < strArr.length; i++) {
                array[i] = (byte) Integer.parseInt(strArr[i], 16);
            }
            return array;
        } else {
            byte[] array = new byte[str.length() / 2];
            int beginIndex = 0;
            for (int i = 0; beginIndex < str.length(); i++) {
                array[i] = (byte) Integer.parseInt(str.substring(beginIndex, beginIndex + 2), 16);
                beginIndex += 2;
            }
            return array;
        }
    }
}

Related

  1. toBytes(String name)
  2. toBytes(String s)
  3. toBytes(String s, int len, byte pad)
  4. toBytes(String str, byte[] bytes, int index)
  5. toBytes4(int n)
  6. toBytesBE(long v)
  7. toBytesBigEndian(long value, int sizeInByte)
  8. toBytesBinary(String in)
  9. toBytesDirect(final String singleOctets)