zoukankan      html  css  js  c++  java
  • java字节处理的各种工具类

         最近工作压力颇大,连续加班一个月,让我这个刚出校园的新人倍感烦恼。有时候真想喊一句“我是自由的”,然后对加班的老板温柔的说句“拜拜了您”就大步流星的走出公司。不过想象终归是想象,毕竟还是要生存的,不工作就没有钱,没钱就没吃的穿的,哎。。好在boss也是个高等知识分子,容易在每天不断的思考中自我反省然后找出问题的所在。这周开了个会议让大家发表对她的看法,大家委婉的道出了对加班的不满,结果现在boss已经能够做到到点自己先走,但我们却俨然被培养成了加班的习惯,欲哭无泪啊。

      话说我自己其实不该有这么多的抱怨,因为这段时间虽是苦了点,但个人的成长速度也惊人,先是掌握了前端的各种知识,可以做出一些闪亮的前端特效,以及超级神奇的可以随意改动大家们做的东西的代码(这里是指前端的东西)去实现自己想要的效果,不禁有点小小窃喜。要知道人一旦拥有了较强的自我学习能力,那么他的进步会是很强大的。之后又趁势帮助一位研究生的同学搞定了单通道图片的合成问题,完善了与硬件交互的通讯代码(使用java udp 的方法,这种不可靠交付的协议还是需要仔细的去分析收集到的数据),最近又华丽丽的完成了对指纹仪的相关操作,那个成就感大家搞技术的都懂的。好了不多说了贴代码,以下的代码是自己最近一段时间能整理和书写的使用java与硬件交互必用的字节处理方法。

    import java.util.ArrayList;
    import java.util.List;
    /**
     * 字节处理的工具类
     * @author wq
     *
     */
    public class ByteDisposeUtil {
        /**
         * 将字符创转化为字节数组(只适合16进制)
         * @param zhiling
         * @return
         */
        public static byte[] conversionStringToBytes(String zhiling){
             List<byte[]> bytes = new ArrayList<byte[]>();
            for(int i=0;i<zhiling.length();i+=2){
                String hex=zhiling.substring(i,i+2);
                byte[] bnew=    hexStringToBytes(hex);
                bytes.add(bnew);
            }
            byte[] newByte = sysCopy(bytes);
            return newByte;
        }
        /**
         *  把data中从off开始的 length个字符转换成十六进制
         * @param data
         * @param off
         * @param length
         * @return
         */
        public static final StringBuffer toHex(byte[] data, int off, int length) {
            StringBuffer buf = new StringBuffer(data.length * 2);
            for (int i = off; i < length; i++) {
                if (((int) data[i] & 0xff) < 0x10) {
                    buf.append("0");
                }
                buf.append(Long.toString((int) data[i] & 0xff, 16));
            }
            return buf;
        }
        /**
         * 字符转化为字节(将10进制转化为16进制后再变为字节)
         * @param c
         * @return
         */
        public static byte charToByte(char c) {  
                return (byte) "0123456789ABCDEF".indexOf(c);  
            }  
        /**
         * 十六进制转化为字节
         * @param hexString
         * @return
         */
        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;  
        } 
        /**
         * 合并数组
         * @param srcArrays
         * @return
         */
        public static byte[] sysCopy(List<byte[]> srcArrays) {
              int len = 0;
              for (byte[] srcArray:srcArrays) {
               len+= srcArray.length;
              }
                 byte[] destArray = new byte[len];   
                 int destLen = 0;
                 for (byte[] srcArray:srcArrays) {
                     System.arraycopy(srcArray, 0, destArray, destLen, srcArray.length);   
                     destLen += srcArray.length;   
                 }   
                 return destArray;
             } 
          /** 
         * 将一个int数据转为按小端顺序排列的字节数组 
         * @param data int数据 
         * @return  按小端顺序排列的字节数组 
         */  
        public  static byte[] changeByte(int data){  
            byte b4 = (byte)((data)>>24);  
            byte b3 = (byte)(((data)<<8)>>24);  
            byte b2= (byte)(((data)<<16)>>24);  
            byte b1 = (byte)(((data)<<24)>>24);  
            byte[] bytes = {b1,b2,b3,b4};  
            return bytes;  
        }  
        /**
         * 十进制转化为十六进制
         * 
         */
        public static String dtoh(int a){
            String bb="";
         while((a/16)!=0){
             int b=a%16;
             String s=""+b;
             if(b==10){
                 s="A";
             }
             if(b==11){
                 s="B";
             }
             if(b==12){
                 s="C";
             }
             if(b==13){
                 s="D";
             }
             if(b==14){
                 s="E";
             }
             if(b==15){
                 s="F";
             }
             a=a/16;
             bb+=s;
         }
           int k=a%16;
        if(k>=10){
         if(k==10){
          bb+="A";
         }
         if(k==11){
          bb+="B";
         }
         if(k==12){
          bb+="C";
         }
         if(k==13){
          bb+="D";
         }
         if(k==14){
          bb+="E";
         }
         if(k==15){
          bb+="F";
         }
      }
      else{
       bb+=k;
      }

    String result
    =new StringBuffer(bb).reverse().toString(); int length=result.length(); for(int j=0;j<4-length;j++){ result="0"+result; } return result; } /** * 16进制转10进制(8位) */ public static String htod(String a){ StringBuffer icid=new StringBuffer(a); String fanzhuan=icid.reverse().toString().toUpperCase(); int zhengshuka=0; for(int i=0;i<fanzhuan.length();i++){ int num="0123456789ABCDEF".indexOf(fanzhuan.charAt(i)); zhengshuka+=(num*Math.pow(16,i)); } String zhengshu=""+zhengshuka; for(int j=0;j<8-(zhengshuka+"").length();j++){ zhengshu=("0"+zhengshu); } return zhengshu; } }

     代码的说明注释都有了,具体不再阐述,大家可以尽情试用,都是非常好用的哦。

  • 相关阅读:
    git 命令手册
    leetcode #7 revert integer 问题
    leetcode #1 twoSum问题:简单实用哈希表
    c++模板函数分离编译的问题
    matlab 与c/c++ 混合MEX的编程
    springboot项目打war包
    springboot-jpa多数据源
    springboot使用RestTemplate+httpclient连接池发送http消息
    IDEA如何安装lombok
    Springboot如何启用文件上传功能
  • 原文地址:https://www.cnblogs.com/wq123/p/3444030.html
Copyright © 2011-2022 走看看