All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.andyshao.lang.ConvertStr2Byte Maven / Gradle / Ivy

The newest version!
package com.github.andyshao.lang;

import java.util.Objects;

/**
 * 
 * Descript:
* Copyright: Copyright(c) Aug 7, 2014
* Encoding:UNIX UTF-8 * * @author Andy.Shao * */ public class ConvertStr2Byte implements Convert { static byte charToByte(char c) { return (byte) "0123456789ABCDEF".indexOf(c); } /** * hex string to bytes * @param hexString hex string * @return {@link ArrayType#BYTE_ARRAY} */ public static byte[] hexStringTobytes(String hexString) { if (Objects.isNull(hexString) || hexString.isEmpty()) return null; hexString = hexString.toUpperCase(); int length = hexString.length() / 2; char[] hexChars = hexString.toCharArray(); byte[] d = new byte[length]; for (int i = 0 ; i < length ; i++) { int pos = i * 2; d[i] = (byte) (char) (ConvertStr2Byte.charToByte(hexChars[pos]) << 4 | ConvertStr2Byte.charToByte(hexChars[pos + 1])); } return d; } /** * hex string to bytes * @param hexString hex string * @return {@link ArrayType#BYTE_ARRAY} */ public static Byte[] hexStringToBytes(String hexString) { if (Objects.isNull(hexString) || hexString.isEmpty()) return null; hexString = hexString.toUpperCase(); int length = hexString.length() / 2; char[] hexChars = hexString.toCharArray(); Byte[] d = new Byte[length]; for (int i = 0 ; i < length ; i++) { int pos = i * 2; d[i] = (byte) (char) (ConvertStr2Byte.charToByte(hexChars[pos]) << 4 | ConvertStr2Byte.charToByte(hexChars[pos + 1])); } return d; } @Override public Byte[] convert(String in) { return ConvertStr2Byte.hexStringToBytes(in); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy