zoukankan      html  css  js  c++  java
  • Java byte转换工具类

    前言

      记录byte转换工具类

    代码

    package net.wt.gate.ui.activity.bleLight;
    
    public class TransformUtil {
    
        /*
         * Convert byte[] to hex string.这里我们可以将byte转换成int,然后利用Integer.toHexString(int)来转换成16进制字符串。
         * @param src byte[] data
         * @return hex string
         */
        public static String bytesToHexString(byte[] src) {
            StringBuilder stringBuilder = new StringBuilder("");
            if (src == null || src.length <= 0) {
                return null;
            }
            for (int i = 0; i < src.length; i++) {
                int v = src[i] & 0xFF;
                String hv = Integer.toHexString(v);
                if (hv.length() < 2) {
                    stringBuilder.append(0);
                }
                stringBuilder.append(hv);
            }
            return stringBuilder.toString();
        }
    
        /**
         * Convert hex string to byte[]
         *
         * @param hexString the hex string
         * @return byte[]
         */
        public static byte[] hexStringToBytes(String hexString) {
            if (hexString == null || hexString.equals("")) {
                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) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
            }
            return d;
        }
    
        public static byte[] int2Bytes(int num) {
            byte[] byteNum = new byte[4];
            for (int ix = 0; ix < 4; ++ix) {
                int offset = 32 - (ix + 1) * 8;
                byteNum[ix] = (byte) ((num >> offset) & 0xff);
            }
            return byteNum;
        }
    
        public static int bytes2Int(byte[] byteNum) {
            int num = 0;
            for (int ix = 0; ix < 4; ++ix) {
                num <<= 8;
                num |= (byteNum[ix] & 0xff);
            }
            return num;
        }
    
        public static byte int2OneByte(int num) {
            return (byte) (num & 0x000000ff);
        }
    
        public static int oneByte2Int(byte byteNum) {
            return byteNum > 0 ? byteNum : (128 + (128 + byteNum));
        }
    
        public static byte[] long2Bytes(long num) {
            byte[] byteNum = new byte[8];
            for (int ix = 0; ix < 8; ++ix) {
                int offset = 64 - (ix + 1) * 8;
                byteNum[ix] = (byte) ((num >> offset) & 0xff);
            }
            return byteNum;
        }
    
        public static long bytes2Long(byte[] byteNum) {
            long num = 0;
            for (int ix = 0; ix < 8; ++ix) {
                num <<= 8;
                num |= (byteNum[ix] & 0xff);
            }
            return num;
        }
    
        /**
         * Convert char to byte
         *
         * @param c char
         * @return byte
         */
        private static byte charToByte(char c) {
            return (byte) "0123456789ABCDEF".indexOf(c);
        }
    }
  • 相关阅读:
    Appium Inspector定位元素与录制简单脚本
    Appium(Python)API
    Appium(Python)驱动手机Chrome浏览器
    Appium(Python)驱动手机淘宝App
    uiautomatorviewer定位App元素
    Android Studio怎样创建App项目
    adb获取设备的序列号
    获取App的PackageName包名和LauncherActivity启动页
    怎样安装Appium
    最小生成树(MST)
  • 原文地址:https://www.cnblogs.com/guanxinjing/p/14015053.html
Copyright © 2011-2022 走看看