• java对获取的字节数组进行处理


    java对获取的字节数组bytes[]进行处理:

    第一种,直接将该字节数组转换为字符串(部分)

    String content = new String(dp.getData(),0,2); //从位置0开始获取2个字节

    这样,对获取的数据报进行全部转换:

    String content = new String(dp.getData(),0,dp.getLength()); //从位置0开始获取dp.getLength()个长度转换为字符串

    通过获取从任意位置(比如0,x)处开始获取2或者dp.getLength()个字节将其转换为字符串,给予显示

    之后转换为整型或者小数都可以,这是字符串转整型/浮点型的问题了

    第二种办法,

    将字节数组转换为十六进制,之后获取某位置开始的多少位数,再之后将该16进制通过函数或者new String 的方法转换为字符串。这样也可以达到目的

     /**
         * byte[] 转为16进制String
         */
        public static String Bytes2HexString(byte[] b) { 
            String ret = ""; 
            for (int i = 0; i < b.length; i++) { 
                String hex = Integer.toHexString(b[i] & 0xFF); 
                if (hex.length() == 1) { 
                    hex = '0' + hex; 
                } 
                ret += hex.toUpperCase(); 
            } 
            return ret; 
        } 
        
        /**
         * 从一个byte[]数组中截取一部分
         * @param src
         * @param begin
         * @param count
         * @return
         */
        public static byte[] subBytes(byte[] src, int begin, int count) {
            byte[] bs = new byte[count];
            for (int i=begin;i<begin+count; i++) bs[i-begin] = src[i];
            return bs;
        }
        
        //     转化十六进制编码为字符串
        public static String toStringHex(String s)
        {
            byte[] baKeyword = new byte[s.length()/2];
            for(int i = 0; i < baKeyword.length; i++)
            {
              try
              {
                  baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i*2, i*2+2),16));
              }
              catch(Exception e)
              {
                  e.printStackTrace();
              }
            }
         
            try 
            {
                s = new String(baKeyword, "utf-8");//UTF-16le:Not
            } 
            catch (Exception e1) 
            {
                e1.printStackTrace();
            } 
            return s;
        }

    应用:

    String content = new String(dp.getData(),0,dp.getLength());//2
            /*测试字节数组*/
            byte[] data=dp.getData(); 
            String dataStr=Bytes2HexString(data);
            /*测试截取字节数组*/
            byte[] subData=subBytes(data,0,2); //截取两个字节(英文字符)  汉字(三个字节)
            String subDataStr16=Bytes2HexString(subData);
            String subDataStr=new String(subData);//toStringHex(subDataStr16);
            //5关闭资源
            ds.close();
    
            System.out.println(ip+"::" +port+":"+content);
            System.out.println("--字节数组转换为字符串"+dataStr);
            System.out.println("--截取子字节数组转换为16进制字符串"+subDataStr16);
            System.out.println("--截取子字节数组转换为字符串"+subDataStr);

    效果:

    全部代码:

    import java.net.*;
    import java.io.*;
    
    public class udpRecv
    {
        /*
        * 创建UDP传输的接收端
        * 1.建立udp socket服务,因为是要接收数据,必须指明端口号
        * 2,创建数据包,用于存储接收到的数据。方便用数据包对象的方法处理数据
        * 3,使用socket服务的receive方法将接收的数据存储到数据包中
        * 4,通过数据包的方法解析数据包中的数据
        * 5,关闭资源
    
        *抛一个大异常:IOException
        */
        public static void main(String[] args) throws IOException{
            //1,创建udp socket服务
            DatagramSocket ds = new DatagramSocket(12345);
    
            //2,创建数据包
            byte[] buf =new byte[1024];
            DatagramPacket dp =new DatagramPacket(buf,buf.length);
    
            //3,使用接收的方法将数据包存储到数据包中
            ds.receive(dp);//阻塞式
    
            //4.通过数据包对象的方法,解析其中的数据
            String ip = dp.getAddress().getHostAddress();
            int port  = dp.getPort();
            String content = new String(dp.getData(),0,dp.getLength());//2
            /*测试字节数组*/
            byte[] data=dp.getData(); 
            String dataStr=Bytes2HexString(data);
            /*测试截取字节数组*/
            byte[] subData=subBytes(data,0,2); //截取两个字节(英文字符)  汉字(三个字节)
            String subDataStr16=Bytes2HexString(subData);
            String subDataStr=new String(subData);//toStringHex(subDataStr16);
            //5关闭资源
            ds.close();
    
            System.out.println(ip+"::" +port+":"+content);
            System.out.println("--字节数组转换为字符串"+dataStr);
            System.out.println("--截取子字节数组转换为16进制字符串"+subDataStr16);
            System.out.println("--截取子字节数组转换为字符串"+subDataStr);
        }
        
        /**
         * byte[] 转为16进制String
         */
        public static String Bytes2HexString(byte[] b) { 
            String ret = ""; 
            for (int i = 0; i < b.length; i++) { 
                String hex = Integer.toHexString(b[i] & 0xFF); 
                if (hex.length() == 1) { 
                    hex = '0' + hex; 
                } 
                ret += hex.toUpperCase(); 
            } 
            return ret; 
        } 
        
        /**
         * 从一个byte[]数组中截取一部分
         * @param src
         * @param begin
         * @param count
         * @return
         */
        public static byte[] subBytes(byte[] src, int begin, int count) {
            byte[] bs = new byte[count];
            for (int i=begin;i<begin+count; i++) bs[i-begin] = src[i];
            return bs;
        }
        
        //     转化十六进制编码为字符串
        public static String toStringHex(String s)
        {
            byte[] baKeyword = new byte[s.length()/2];
            for(int i = 0; i < baKeyword.length; i++)
            {
              try
              {
                  baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i*2, i*2+2),16));
              }
              catch(Exception e)
              {
                  e.printStackTrace();
              }
            }
         
            try 
            {
                s = new String(baKeyword, "utf-8");//UTF-16le:Not
            } 
            catch (Exception e1) 
            {
                e1.printStackTrace();
            } 
            return s;
        }
    
    }
    udpS对字节数组处理
    import java.net.*;
    import java.io.*;
    
    public class udpSend
    {
        /*
         *记得抛异常
        */
        public static void main(String[] args) throws IOException{
            
            System.out.println("发送端启动...");
            /*
            *创建UDP传输的发送端
            * 思路:
            * 1.建立udp的socket服务(new socket)
            * 2,将要发送的数据封装到数据包中。(packet)
            * 3,通过udp的socket服务将数据包发送出去(send)
            * 4,关闭socket服务(close)
    
            **抛一个大异常:IOException
            */
    
            //1.udpsocket服务对象,使用DatagramSocket创建,可以指明本地IP和端口
            DatagramSocket ds = new DatagramSocket(8888);
    
            //2.将要发送的数据封装到数据包中
            String str ="12.345";//Hello--12.345
            byte[] buf =str.getBytes();
            DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),12345);//192.168.0.5
    
            //3.udp发送,使用socket服务将数据包发送出去
            ds.send(dp);
    
            //4.关闭连接
            ds.close();
    
            
        }
    }
    udpSend

    总结:

    其实,首先根据dp.getContent()获取的字节数组后,最简单的办法就是String str=

    String(dp.getData(),x,y);即可搞定所有的,需要考虑的问题是编码问题
  • 相关阅读:
    Debian 7 amd64 + fbterm + ucimf
    golanggorilla/mux,强大的URL路由和调度器
    Debian 7 amd64问题
    在mysql启用远程连接
    golang从类型转换角度看interface
    golang监控goroutine异常退出
    golangRedis最佳的Go语言驱动
    usaco Scrambled Letters
    usaco Milk Routing
    滚动数组出错的原因都有那些?&poj3254
  • 原文地址:https://www.cnblogs.com/shuqingstudy/p/5043182.html
Copyright © 2020-2023  润新知