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; } }

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(); } }
總結:
其實,首先根據dp.getContent()獲取的字節數組后,最簡單的辦法就是String str=
String(dp.getData(),x,y);即可搞定所有的,需要考慮的問題是編碼問題