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

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

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

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

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

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

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

第二种办法,

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

  1. /**
  2. * byte[] 转为16进制String
  3. */
  4. public static String Bytes2HexString(byte[] b) {
  5. String ret = "";
  6. for (int i = 0; i < b.length; i++) {
  7. String hex = Integer.toHexString(b[i] & 0xFF);
  8. if (hex.length() == 1) {
  9. hex = '0' + hex;
  10. }
  11. ret += hex.toUpperCase();
  12. }
  13. return ret;
  14. }
  15.  
  16. /**
  17. * 从一个byte[]数组中截取一部分
  18. * @param src
  19. * @param begin
  20. * @param count
  21. * @return
  22. */
  23. public static byte[] subBytes(byte[] src, int begin, int count) {
  24. byte[] bs = new byte[count];
  25. for (int i=begin;i<begin+count; i++) bs[i-begin] = src[i];
  26. return bs;
  27. }
  28.  
  29. // 转化十六进制编码为字符串
  30. public static String toStringHex(String s)
  31. {
  32. byte[] baKeyword = new byte[s.length()/2];
  33. for(int i = 0; i < baKeyword.length; i++)
  34. {
  35. try
  36. {
  37. baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i*2, i*2+2),16));
  38. }
  39. catch(Exception e)
  40. {
  41. e.printStackTrace();
  42. }
  43. }
  44.  
  45. try
  46. {
  47. s = new String(baKeyword, "utf-8");//UTF-16le:Not
  48. }
  49. catch (Exception e1)
  50. {
  51. e1.printStackTrace();
  52. }
  53. return s;
  54. }

应用:

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

效果:

全部代码:

  1. import java.net.*;
  2. import java.io.*;
  3.  
  4. public class udpRecv
  5. {
  6. /*
  7. * 创建UDP传输的接收端
  8. * 1.建立udp socket服务,因为是要接收数据,必须指明端口号
  9. * 2,创建数据包,用于存储接收到的数据。方便用数据包对象的方法处理数据
  10. * 3,使用socket服务的receive方法将接收的数据存储到数据包中
  11. * 4,通过数据包的方法解析数据包中的数据
  12. * 5,关闭资源
  13.  
  14. *抛一个大异常:IOException
  15. */
  16. public static void main(String[] args) throws IOException{
  17. //1,创建udp socket服务
  18. DatagramSocket ds = new DatagramSocket(12345);
  19.  
  20. //2,创建数据包
  21. byte[] buf =new byte[1024];
  22. DatagramPacket dp =new DatagramPacket(buf,buf.length);
  23.  
  24. //3,使用接收的方法将数据包存储到数据包中
  25. ds.receive(dp);//阻塞式
  26.  
  27. //4.通过数据包对象的方法,解析其中的数据
  28. String ip = dp.getAddress().getHostAddress();
  29. int port = dp.getPort();
  30. String content = new String(dp.getData(),0,dp.getLength());//
  31. /*测试字节数组*/
  32. byte[] data=dp.getData();
  33. String dataStr=Bytes2HexString(data);
  34. /*测试截取字节数组*/
  35. byte[] subData=subBytes(data,0,2); //截取两个字节(英文字符) 汉字(三个字节)
  36. String subDataStr16=Bytes2HexString(subData);
  37. String subDataStr=new String(subData);//toStringHex(subDataStr16);
  38. //5关闭资源
  39. ds.close();
  40.  
  41. System.out.println(ip+"::" +port+":"+content);
  42. System.out.println("--字节数组转换为字符串"+dataStr);
  43. System.out.println("--截取子字节数组转换为16进制字符串"+subDataStr16);
  44. System.out.println("--截取子字节数组转换为字符串"+subDataStr);
  45. }
  46.  
  47. /**
  48. * byte[] 转为16进制String
  49. */
  50. public static String Bytes2HexString(byte[] b) {
  51. String ret = "";
  52. for (int i = 0; i < b.length; i++) {
  53. String hex = Integer.toHexString(b[i] & 0xFF);
  54. if (hex.length() == 1) {
  55. hex = '0' + hex;
  56. }
  57. ret += hex.toUpperCase();
  58. }
  59. return ret;
  60. }
  61.  
  62. /**
  63. * 从一个byte[]数组中截取一部分
  64. * @param src
  65. * @param begin
  66. * @param count
  67. * @return
  68. */
  69. public static byte[] subBytes(byte[] src, int begin, int count) {
  70. byte[] bs = new byte[count];
  71. for (int i=begin;i<begin+count; i++) bs[i-begin] = src[i];
  72. return bs;
  73. }
  74.  
  75. // 转化十六进制编码为字符串
  76. public static String toStringHex(String s)
  77. {
  78. byte[] baKeyword = new byte[s.length()/2];
  79. for(int i = 0; i < baKeyword.length; i++)
  80. {
  81. try
  82. {
  83. baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i*2, i*2+2),16));
  84. }
  85. catch(Exception e)
  86. {
  87. e.printStackTrace();
  88. }
  89. }
  90.  
  91. try
  92. {
  93. s = new String(baKeyword, "utf-8");//UTF-16le:Not
  94. }
  95. catch (Exception e1)
  96. {
  97. e1.printStackTrace();
  98. }
  99. return s;
  100. }
  101.  
  102. }

udpS对字节数组处理

  1. import java.net.*;
  2. import java.io.*;
  3.  
  4. public class udpSend
  5. {
  6. /*
  7. *记得抛异常
  8. */
  9. public static void main(String[] args) throws IOException{
  10.  
  11. System.out.println("发送端启动...");
  12. /*
  13. *创建UDP传输的发送端
  14. * 思路:
  15. * 1.建立udp的socket服务(new socket)
  16. * 2,将要发送的数据封装到数据包中。(packet)
  17. * 3,通过udp的socket服务将数据包发送出去(send)
  18. * 4,关闭socket服务(close)
  19.  
  20. **抛一个大异常:IOException
  21. */
  22.  
  23. //1.udpsocket服务对象,使用DatagramSocket创建,可以指明本地IP和端口
  24. DatagramSocket ds = new DatagramSocket(8888);
  25.  
  26. //2.将要发送的数据封装到数据包中
  27. String str ="12.345";//Hello--12.345
  28. byte[] buf =str.getBytes();
  29. DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),12345);//192.168.0.5
  30.  
  31. //3.udp发送,使用socket服务将数据包发送出去
  32. ds.send(dp);
  33.  
  34. //4.关闭连接
  35. ds.close();
  36.  
  37. }
  38. }

udpSend

总结:

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

  1. String(dp.getData(),x,y);即可搞定所有的,需要考虑的问题是编码问题

java对获取的字节数组进行处理的更多相关文章

  1. Java将文件转为字节数组

    Java将文件转为字节数组 关键字:文件,文件流,字节流,字节数组,二进制 摘要:最近工作中碰到的需求是,利用http传输二进制数据到服务器对应接口,需要传输userId, file(加密后)等一系列 ...

  2. Java文件与io——字节数组流数据流字符串流

    字节数组流 ByteArrayInputStream:包含一个内部缓冲区,该缓冲区包含从流中读取的字节.内部计数器跟踪read方法要提供的下一个字节.关闭ByteArrayInputStream无效. ...

  3. java 读取文件的字节数组

    /*文件64位编码*/ public static void main(String[] args) {    byte[] fileByte = toByteArray(newFile);   St ...

  4. Java中文件与字节数组转换

    注:来源于JavaEye 文件转化为字节数组: http://www.javaeye.com/topic/304980 /** * 文件转化为字节数组 * * @param file * @retur ...

  5. 【Java】获取二维数组行列长度

    二维数组int array[][] = new int[3][3]; 行长度:array.length 列长度:array[i].length

  6. C#获取文件/字节数组MD5值方法

    找了很多,就这个管用,有时间好好研究一番 public static string GetMD5Hash(string fileName) { try { FileStream file = new ...

  7. JAVA把InputStream 转 字节数组(byte[])

    import org.apache.commons.io.IOUtils; byte[] bytes = IOUtils.toByteArray(inputStream); 如果没有这个包 就加下依赖 ...

  8. 【Java】字节数组转换工具类

    import org.apache.commons.lang.ArrayUtils; import java.nio.charset.Charset; /** * 字节数组转换工具类 */ publi ...

  9. java对象转字节数组,获取泛型类

    对象转字节数组,字节数组在恢复成对象 Test.java class Test { public static void main(String args[]) throws IOException, ...

随机推荐

  1. 自己动手编译octave 4.0.0

    今天在做作业的时候,发现imread不能使用,说要安装相应的图形包,可是要安装image时,却发现要求4.0.0版本,而我本机的linux系统ubuntu15.04只有3.8.x的安装源,没办法,只能 ...

  2. UVa11555 - Aspen Avenue

    今晚CF GYM A题,神坑.. 原题: Aspen Avenue ``Phew, that was the last one!'' exclaimed the garden helper Tim a ...

  3. ubuntu升级php版本

    如果安装的 PHP 版本过低的话,可以通过下面的指令来升级: sudo add-apt-repository ppa:ondrej/php5   sudo apt-get update   sudo  ...

  4. html页面头部里的meta

    作者:zccst html中的http-equiv属性应用详解 一.简介 http-equiv 属性 -- HTTP协议的响应头报文 此属性出现在meta标签中 此属性用于代替name,HTTP服务器 ...

  5. LPC1768IAP(详解,有上位机)

    之前说了stm32的iap编程,今天天气真好,顺手就来说说lpc1788的iap编程(没看前面的请查看stm笔记下的内容) 首先是flash的算法,lpc1768并没有寄存器来让我们操作flash,他 ...

  6. 安卓主activity引用自定义的View——Android LayoutInflater原理分析

    相信接触Android久一点的朋友对于LayoutInflater一定不会陌生,都会知道它主要是用于加载布局的.而刚接触Android的朋友可能对LayoutInflater不怎么熟悉,因为加载布局的 ...

  7. 笔记-Python基础教程(第二版)第一章

    第一章 快速改造:基础知识 01:整除.乘方 (Python3.0之前 如2.7版本) >>> 1/2 ==>0 1/2整除,普通除法: 解决办法1: 1.0/2.0  ==& ...

  8. 使用jquery时一些小技巧的总结

    使用 each 遍历 var nodes = Ztree.getCheckedNodes(true); //获取所有勾选的节点 $.each(nodes,function(i,value){ aler ...

  9. input框的默认bug解决办法

    input框的默认bug是在没干掉边框的情况下是不能设置背景颜色的,否则边框会变成内边框(黑色)效果,很难看. 解决办法是: none掉input框的边框:border:none; 再设置其背景色为任 ...

  10. eclipse 标签标题乱码解决方法

    一般出现此类问题都是由于更改本地语言设置引起的. 解决办法: 1.恢复到原来默认的语言和地域 2.更改eclipse主题(Window-->preferences-->General--& ...