123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895 |
- package com.inet.ailink.receiver.common.utils;
-
- import org.apache.commons.lang.ArrayUtils;
-
- import java.nio.charset.Charset;
-
- /**
- *
- * @Description: 字节数组转换工具类
- * @author fun
- * @date 2019年3月27日
- */
- public class BytesUtils {
-
- public static final String GBK = "GBK";
- public static final String UTF8 = "utf-8";
- public static final char[] ascii = "0123456789ABCDEF".toCharArray();
- private static char[] HEX_VOCABLE = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
-
- /**
- * 将short整型数值转换为字节数组
- *
- * @param data
- * @return
- */
- public static byte[] getBytes(short data) {
- byte[] bytes = new byte[2];
- bytes[0] = (byte) ((data & 0xff00) >> 8);
- bytes[1] = (byte) (data & 0xff);
- return bytes;
- }
-
- /**
- * 将字符转换为字节数组
- *
- * @param data
- * @return
- */
- public static byte[] getBytes(char data) {
- byte[] bytes = new byte[2];
- bytes[0] = (byte) (data >> 8);
- bytes[1] = (byte) (data);
- return bytes;
- }
-
- /**
- * 将布尔值转换为字节数组
- *
- * @param data
- * @return
- */
- public static byte[] getBytes(boolean data) {
- byte[] bytes = new byte[1];
- bytes[0] = (byte) (data ? 1 : 0);
- return bytes;
- }
-
- /**
- * 将整型数值转换为字节数组
- *
- * @param data
- * @return
- */
- public static byte[] getBytes(int data) {
- byte[] bytes = new byte[4];
- bytes[0] = (byte) ((data & 0xff000000) >> 24);
- bytes[1] = (byte) ((data & 0xff0000) >> 16);
- bytes[2] = (byte) ((data & 0xff00) >> 8);
- bytes[3] = (byte) (data & 0xff);
- return bytes;
- }
-
- /**
- * 将long整型数值转换为字节数组
- *
- * @param data
- * @return
- */
- public static byte[] getBytes(long data) {
- byte[] bytes = new byte[8];
- bytes[0] = (byte) ((data >> 56) & 0xff);
- bytes[1] = (byte) ((data >> 48) & 0xff);
- bytes[2] = (byte) ((data >> 40) & 0xff);
- bytes[3] = (byte) ((data >> 32) & 0xff);
- bytes[4] = (byte) ((data >> 24) & 0xff);
- bytes[5] = (byte) ((data >> 16) & 0xff);
- bytes[6] = (byte) ((data >> 8) & 0xff);
- bytes[7] = (byte) (data & 0xff);
- return bytes;
- }
-
- /**
- * 将float型数值转换为字节数组
- *
- * @param data
- * @return
- */
- public static byte[] getBytes(float data) {
- int intBits = Float.floatToIntBits(data);
- return getBytes(intBits);
- }
-
- /**
- *
- * @Title: getBytes
- * @Description: 将double型数值转换为字节数组
- * @return byte[]
- * @author fun
- * @date 2019年3月27日
- */
-
- public static byte[] getBytes(double data) {
- long intBits = Double.doubleToLongBits(data);
- return getBytes(intBits);
- }
-
- /**
- *
- * @Title: getBytes
- * @Description: 将字符串按照charsetName编码格式的字节数组
- * @return byte[]
- * @param data 字符串
- * @param charsetName
- * @author fun
- * @date 2019年3月27日
- */
- public static byte[] getBytes(String data, String charsetName) {
- Charset charset = Charset.forName(charsetName);
- return data.getBytes(charset);
- }
-
- /**
- *
- * @Title: getBytes
- * @Description: 将字符串按照GBK编码格式的字节数组
- * @return byte[]
- * @author fun
- * @date 2019年3月27日
- */
- public static byte[] getBytes(String data) {
- return getBytes(data, GBK);
- }
-
- /**
- *
- * @Title: getBoolean
- * @Description: 将字节数组第0字节转换为布尔值
- * @return boolean
- * @author fun
- * @date 2019年3月27日
- */
- public static boolean getBoolean(byte[] bytes) {
- return bytes[0] == 1;
- }
-
- /**
- *
- * @Title: getBoolean
- * @Description: 将字节数组的第index字节转换为布尔值
- * @return boolean
- * @author fun
- * @date 2019年3月27日
- */
- public static boolean getBoolean(byte[] bytes, int index) {
- return bytes[index] == 1;
- }
-
- /**
- *
- * @Title: getShort
- * @Description: 将字节数组前2字节转换为short整型数值
- * @return short
- * @author fun
- * @date 2019年3月27日
- */
- public static short getShort(byte[] bytes) {
- return (short) ((0xff00 & (bytes[0] << 8)) | (0xff & bytes[1]));
- }
-
- /**
- *
- * @Title: getShort
- * @Description: 将字节数组从startIndex开始的2个字节转换为short整型数值
- * @return short
- * @author fun
- * @date 2019年3月27日
- */
- public static short getShort(byte[] bytes, int startIndex) {
- return (short) ((0xff00 & (bytes[startIndex] << 8)) | (0xff & bytes[startIndex + 1]));
- }
-
- /**
- *
- * @Title: getChar
- * @Description: 将字节数组前2字节转换为字符
- * @return char
- * @author fun
- * @date 2019年3月27日
- */
- public static char getChar(byte[] bytes) {
- return (char) ((0xff00 & (bytes[0] << 8)) | (0xff & bytes[1]));
- }
-
- /**
- *
- * @Title: getChar
- * @Description: 将字节数组从startIndex开始的2个字节转换为字符
- * @return char
- * @author fun
- * @date 2019年3月27日
- */
- public static char getChar(byte[] bytes, int startIndex) {
- return (char) ((0xff00 & (bytes[startIndex] << 8)) | (0xff & bytes[startIndex + 1]));
- }
-
- /**
- *
- * @Title: getInt
- * @Description: 将字节数组前4字节转换为整型数值
- * @return int
- * @author fun
- * @date 2019年3月27日
- */
- public static int getInt(byte[] bytes) {
- return (0xff000000 & (bytes[0] << 24) | (0xff0000 & (bytes[1] << 16)) | (0xff00 & (bytes[2] << 8))
- | (0xff & bytes[3]));
- }
-
- /**
- *
- * @Title: getInt
- * @Description: 将字节数组从startIndex开始的4个字节转换为整型数值
- * @return int
- * @author fun
- * @date 2019年3月27日
- */
- public static int getInt(byte[] bytes, int startIndex) {
- return (0xff000000 & (bytes[startIndex] << 24) | (0xff0000 & (bytes[startIndex + 1] << 16))
- | (0xff00 & (bytes[startIndex + 2] << 8)) | (0xff & bytes[startIndex + 3]));
- }
-
- /**
- * 将字节数组前8字节转换为long整型数值
- *
- * @param bytes
- * @return
- */
- public static long getLong(byte[] bytes) {
- return (0xff00000000000000L & ((long) bytes[0] << 56) | (0xff000000000000L & ((long) bytes[1] << 48))
- | (0xff0000000000L & ((long) bytes[2] << 40)) | (0xff00000000L & ((long) bytes[3] << 32))
- | (0xff000000L & ((long) bytes[4] << 24)) | (0xff0000L & ((long) bytes[5] << 16))
- | (0xff00L & ((long) bytes[6] << 8)) | (0xffL & (long) bytes[7]));
- }
-
- /**
- * 将字节数组从startIndex开始的8个字节转换为long整型数值
- *
- * @param bytes
- * @param startIndex
- * @return
- */
- public static long getLong(byte[] bytes, int startIndex) {
- return (0xff00000000000000L & ((long) bytes[startIndex] << 56)
- | (0xff000000000000L & ((long) bytes[startIndex + 1] << 48))
- | (0xff0000000000L & ((long) bytes[startIndex + 2] << 40))
- | (0xff00000000L & ((long) bytes[startIndex + 3] << 32))
- | (0xff000000L & ((long) bytes[startIndex + 4] << 24))
- | (0xff0000L & ((long) bytes[startIndex + 5] << 16)) | (0xff00L & ((long) bytes[startIndex + 6] << 8))
- | (0xffL & (long) bytes[startIndex + 7]));
- }
-
- /**
- * 将字节数组前4字节转换为float型数值
- *
- * @param bytes
- * @return
- */
- public static float getFloat(byte[] bytes) {
- return Float.intBitsToFloat(getInt(bytes));
- }
-
- /**
- * 将字节数组从startIndex开始的4个字节转换为float型数值
- *
- * @param bytes
- * @param startIndex
- * @return
- */
- public static float getFloat(byte[] bytes, int startIndex) {
- byte[] result = new byte[4];
- System.arraycopy(bytes, startIndex, result, 0, 4);
- return Float.intBitsToFloat(getInt(result));
- }
-
- /**
- * 将字节数组前8字节转换为double型数值
- *
- * @param bytes
- * @return
- */
- public static double getDouble(byte[] bytes) {
- long l = getLong(bytes);
- return Double.longBitsToDouble(l);
- }
-
- /**
- * 将字节数组从startIndex开始的8个字节转换为double型数值
- *
- * @param bytes
- * @param startIndex
- * @return
- */
- public static double getDouble(byte[] bytes, int startIndex) {
- byte[] result = new byte[8];
- System.arraycopy(bytes, startIndex, result, 0, 8);
- long l = getLong(result);
- return Double.longBitsToDouble(l);
- }
-
- /**
- * 将charsetName编码格式的字节数组转换为字符串
- *
- * @param bytes
- * @param charsetName
- * @return
- */
- public static String getString(byte[] bytes, String charsetName) {
- return new String(bytes, Charset.forName(charsetName));
- }
-
- /**
- * 将GBK编码格式的字节数组转换为字符串
- *
- * @param bytes
- * @return
- */
- public static String getString(byte[] bytes) {
- return getString(bytes, GBK);
- }
-
- /**
- * 将16进制字符串转换为字节数组
- *
- * @param hex
- * @return
- */
- public static byte[] hexStringToBytes(String hex) {
- if (hex == null || "".equals(hex)) {
- return null;
- }
- int len = hex.length() / 2;
- byte[] result = new byte[len];
- char[] chArr = hex.toCharArray();
- for (int i = 0; i < len; i++) {
- int pos = i * 2;
- result[i] = (byte) (toByte(chArr[pos]) << 4 | toByte(chArr[pos + 1]));
- }
- return result;
- }
-
- /**
- * 将16进制字符串转换为字节数组
- *
- * @param hex
- * @return
- */
- public static byte[] hexToBytes(String hex) {
- if (hex.length() % 2 != 0)
- throw new IllegalArgumentException("input string should be any multiple of 2!");
- hex.toUpperCase();
-
- byte[] byteBuffer = new byte[hex.length() / 2];
-
- byte padding = 0x00;
- boolean paddingTurning = false;
- for (int i = 0; i < hex.length(); i++) {
- if (paddingTurning) {
- char c = hex.charAt(i);
- int index = indexOf(hex, c);
- padding = (byte) ((padding << 4) | index);
- byteBuffer[i / 2] = padding;
- padding = 0x00;
- paddingTurning = false;
- } else {
- char c = hex.charAt(i);
- int index = indexOf(hex, c);
- padding = (byte) (padding | index);
- paddingTurning = true;
- }
-
- }
- return byteBuffer;
- }
-
- private static int indexOf(String input, char c) {
- int index = ArrayUtils.indexOf(HEX_VOCABLE, c);
-
- if (index < 0) {
- throw new IllegalArgumentException("err input:" + input);
- }
- return index;
-
- }
-
- /**
- * 将BCD编码的字节数组转换为字符串
- *
- * @param bcds
- * @return
- */
- public static String bcdToString(byte[] bcds) {
- if (bcds == null || bcds.length == 0) {
- return null;
- }
- byte[] temp = new byte[2 * bcds.length];
- for (int i = 0; i < bcds.length; i++) {
- temp[i * 2] = (byte) ((bcds[i] >> 4) & 0x0f);
- temp[i * 2 + 1] = (byte) (bcds[i] & 0x0f);
- }
- StringBuffer res = new StringBuffer();
- for (int i = 0; i < temp.length; i++) {
- res.append(ascii[temp[i]]);
- }
- return res.toString();
- }
-
- /**
- * 字节转整形
- *
- * @param value
- * @return
- */
- public static int bcdToInt(byte value) {
- return ((value >> 4) * 10) + (value & 0x0F);
- }
-
- /**
- * 字节数组转16进制字符串
- *
- * @param bs
- * @return
- */
- public static String bytesToHex(byte[] bs) {
- StringBuilder sb = new StringBuilder();
- for (byte b : bs) {
- int high = (b >> 4) & 0x0f;
- int low = b & 0x0f;
- sb.append(HEX_VOCABLE[high]);
- sb.append(HEX_VOCABLE[low]);
- }
- return sb.toString();
- }
-
- /**
- * 字节数组取前len个字节转16进制字符串
- *
- * @param bs
- * @param len
- * @return
- */
- public static String bytesToHex(byte[] bs, int len) {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < len; i++) {
- byte b = bs[i];
- int high = (b >> 4) & 0x0f;
- int low = b & 0x0f;
- sb.append(HEX_VOCABLE[high]);
- sb.append(HEX_VOCABLE[low]);
- }
- return sb.toString();
- }
-
- /**
- * 字节数组偏移offset长度之后的取len个字节转16进制字符串
- *
- * @param bs
- * @param offset
- * @param len
- * @return
- */
- public static String bytesToHex(byte[] bs, int offset, int len) {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < len; i++) {
- byte b = bs[offset + i];
- int high = (b >> 4) & 0x0f;
- int low = b & 0x0f;
- sb.append(HEX_VOCABLE[high]);
- sb.append(HEX_VOCABLE[low]);
- }
- return sb.toString();
- }
-
- /**
- * 字节转16进制字符串
- *
- * @param bs
- * @return
- */
- public static String byteToHex(byte b) {
- StringBuilder sb = new StringBuilder();
- int high = (b >> 4) & 0x0f;
- int low = b & 0x0f;
- sb.append(HEX_VOCABLE[high]);
- sb.append(HEX_VOCABLE[low]);
- String s = sb.toString();
- if ("".equals(s.substring(0, 1))) {
- return s.substring(1);
- } else {
- return s;
- }
- }
-
- /**
- * 将字节数组取反
- *
- * @param src
- * @return
- */
- public static String negate(byte[] src) {
- if (src == null || src.length == 0) {
- return null;
- }
- byte[] temp = new byte[2 * src.length];
- for (int i = 0; i < src.length; i++) {
- byte tmp = (byte) (0xFF ^ src[i]);
- temp[i * 2] = (byte) ((tmp >> 4) & 0x0f);
- temp[i * 2 + 1] = (byte) (tmp & 0x0f);
- }
- StringBuffer res = new StringBuffer();
- for (int i = 0; i < temp.length; i++) {
- res.append(ascii[temp[i]]);
- }
- return res.toString();
- }
-
- /**
- * 比较字节数组是否相同
- *
- * @param a
- * @param b
- * @return
- */
- public static boolean compareBytes(byte[] a, byte[] b) {
- if (a == null || a.length == 0 || b == null || b.length == 0 || a.length != b.length) {
- return false;
- }
- if (a.length == b.length) {
- for (int i = 0; i < a.length; i++) {
- if (a[i] != b[i]) {
- return false;
- }
- }
- } else {
- return false;
- }
- return true;
- }
-
- /**
- * 只比对指定长度byte
- *
- * @param a
- * @param b
- * @param len
- * @return
- */
- public static boolean compareBytes(byte[] a, byte[] b, int len) {
- if (a == null || a.length == 0 || b == null || b.length == 0 || a.length < len || b.length < len) {
- return false;
- }
- for (int i = 0; i < len; i++) {
- if (a[i] != b[i]) {
- return false;
- }
- }
- return true;
- }
-
- /**
- * 将字节数组转换为二进制字符串
- *
- * @param items
- * @return
- */
- public static String bytesToBinaryString(byte[] items) {
- if (items == null || items.length == 0) {
- return null;
- }
- StringBuffer buf = new StringBuffer();
- for (byte item : items) {
- buf.append(byteToBinaryString(item));
- }
- return buf.toString();
- }
-
- /**
- * 将字节转换为二进制字符串
- *
- * @param items
- * @return
- */
- public static String byteToBinaryString(byte item) {
- byte a = item;
- StringBuffer buf = new StringBuffer();
- for (int i = 0; i < 8; i++) {
- buf.insert(0, a % 2);
- a = (byte) (a >> 1);
- }
- return buf.toString();
- }
-
- /**
- * 对数组a,b进行异或运算
- *
- * @param a
- * @param b
- * @return
- */
- public static byte[] xor(byte[] a, byte[] b) {
- if (a == null || a.length == 0 || b == null || b.length == 0 || a.length != b.length) {
- return null;
- }
- byte[] result = new byte[a.length];
- for (int i = 0; i < a.length; i++) {
- result[i] = (byte) (a[i] ^ b[i]);
- }
- return result;
- }
-
- /**
- * 对数组a,b进行异或运算 运算长度len
- *
- * @param a
- * @param b
- * @param len
- * @return
- */
- public static byte[] xor(byte[] a, byte[] b, int len) {
- if (a == null || a.length == 0 || b == null || b.length == 0) {
- return null;
- }
- if (a.length < len || b.length < len) {
- return null;
- }
- byte[] result = new byte[len];
- for (int i = 0; i < len; i++) {
- result[i] = (byte) (a[i] ^ b[i]);
- }
- return result;
- }
-
- /**
- * 将short整型数值转换为字节数组
- *
- * @param num
- * @return
- */
- public static byte[] shortToBytes(int num) {
- byte[] temp = new byte[2];
- for (int i = 0; i < 2; i++) {
- temp[i] = (byte) ((num >>> (8 - i * 8)) & 0xFF);
- }
- return temp;
- }
-
- /**
- * 将字节数组转为整型
- *
- * @param num
- * @return
- */
- public static int bytesToShort(byte[] arr) {
- int mask = 0xFF;
- int temp = 0;
- int result = 0;
- for (int i = 0; i < 2; i++) {
- result <<= 8;
- temp = arr[i] & mask;
- result |= temp;
- }
- return result;
- }
-
- /**
- * 将整型数值转换为指定长度的字节数组
- *
- * @param num
- * @return
- */
- public static byte[] intToBytes(int num) {
- byte[] temp = new byte[4];
- for (int i = 0; i < 4; i++) {
- temp[i] = (byte) ((num >>> (24 - i * 8)) & 0xFF);
- }
- return temp;
- }
-
- /**
- * 将整型数值转换为指定长度的字节数组
- *
- * @param src
- * @param len
- * @return
- */
- public static byte[] intToBytes(int src, int len) {
- if (len < 1 || len > 4) {
- return null;
- }
- byte[] temp = new byte[len];
- for (int i = 0; i < len; i++) {
- temp[len - 1 - i] = (byte) ((src >>> (8 * i)) & 0xFF);
- }
- return temp;
- }
-
- /**
- * 将字节数组转换为整型数值
- *
- * @param arr
- * @return
- */
- public static int bytesToInt(byte[] arr) {
- int mask = 0xFF;
- int temp = 0;
- int result = 0;
- for (int i = 0; i < 4; i++) {
- result <<= 8;
- temp = arr[i] & mask;
- result |= temp;
- }
- return result;
- }
-
- /**
- * 将long整型数值转换为字节数组
- *
- * @param num
- * @return
- */
- public static byte[] longToBytes(long num) {
- byte[] temp = new byte[8];
- for (int i = 0; i < 8; i++) {
- temp[i] = (byte) ((num >>> (56 - i * 8)) & 0xFF);
- }
- return temp;
- }
-
- /**
- * 将字节数组转换为long整型数值
- *
- * @param arr
- * @return
- */
- public static long bytesToLong(byte[] arr) {
- int mask = 0xFF;
- int temp = 0;
- long result = 0;
- int len = Math.min(8, arr.length);
- for (int i = 0; i < len; i++) {
- result <<= 8;
- temp = arr[i] & mask;
- result |= temp;
- }
- return result;
- }
-
- /**
- * 将16进制字符转换为字节
- *
- * @param c
- * @return
- */
- public static byte toByte(char c) {
- byte b = (byte) "0123456789ABCDEF".indexOf(c);
- return b;
- }
-
- /**
- * 功能描述:把两个字节的字节数组转化为整型数据,高位补零,例如:<br/>
- * 有字节数组byte[] data = new byte[]{1,2};转换后int数据的字节分布如下:<br/>
- * 00000000 00000000 00000001 00000010,函数返回258
- *
- * @param lenData
- * 需要进行转换的字节数组
- * @return 字节数组所表示整型值的大小
- */
- public static int bytesToIntWhereByteLengthEquals2(byte lenData[]) {
- if (lenData.length != 2) {
- return -1;
- }
- byte fill[] = new byte[] { 0, 0 };
- byte real[] = new byte[4];
- System.arraycopy(fill, 0, real, 0, 2);
- System.arraycopy(lenData, 0, real, 2, 2);
- int len = byteToInt(real);
- return len;
-
- }
-
- /**
- * 功能描述:将byte数组转化为int类型的数据
- *
- * @param byteVal
- * 需要转化的字节数组
- * @return 字节数组所表示的整型数据
- */
- public static int byteToInt(byte[] byteVal) {
- int result = 0;
- for (int i = 0; i < byteVal.length; i++) {
- int tmpVal = (byteVal[i] << (8 * (3 - i)));
- switch (i) {
- case 0:
- tmpVal = tmpVal & 0xFF000000;
- break;
- case 1:
- tmpVal = tmpVal & 0x00FF0000;
- break;
- case 2:
- tmpVal = tmpVal & 0x0000FF00;
- break;
- case 3:
- tmpVal = tmpVal & 0x000000FF;
- break;
- }
-
- result = result | tmpVal;
- }
- return result;
- }
-
- public static byte CheckXORSum(byte[] bData) {
- byte sum = 0x00;
- for (int i = 0; i < bData.length; i++) {
- sum ^= bData[i];
- }
- return sum;
- }
-
- /**
- * 从offset开始 将后续长度为len的byte字节转为int
- *
- * @param data
- * @param offset
- * @param len
- * @return
- */
- public static int bytesToInt(byte[] data, int offset, int len) {
- int mask = 0xFF;
- int temp = 0;
- int result = 0;
- len = Math.min(len, 4);
- for (int i = 0; i < len; i++) {
- result <<= 8;
- temp = data[offset + i] & mask;
- result |= temp;
- }
- return result;
- }
-
- /**
- * byte字节数组中的字符串的长度
- *
- * @param data
- * @return
- */
- public static int getBytesStringLen(byte[] data) {
- int count = 0;
- for (byte b : data) {
- if (b == 0x00)
- break;
- count++;
- }
- return count;
- }
-
- /**
- *
- * @Title: hexString2binaryString
- * @Description: 十六进制字符串转二进制字符串
- * @return String
- * @author fun
- * @date 2019年3月27日
- */
- public static String hexString2binaryString(String hexString) {
- if (hexString == null || hexString.length() % 2 != 0)
- return null;
- String bString = "", tmp;
- for (int i = 0; i < hexString.length(); i++) {
- tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
- bString += tmp.substring(tmp.length() - 4);
- }
- return bString;
- }
-
- }
|