![JAR search and dependency download from the Maven repository](/logo.png)
com.github.andyshao.convert.ConvertStr2Byte Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Useful-Tools Show documentation
Show all versions of Useful-Tools Show documentation
Some thing about the useful tools of the JDK 1.8.
The newest version!
package com.github.andyshao.convert;
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);
}
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[] apply(String in) {
return ConvertStr2Byte.hexStringToBytes(in);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy