java对获取的字节数组进行处理
java对获取的字节数组bytes[]进行处理:
第一种,直接将该字节数组转换为字符串(部分):
String content = new String(dp.getData(),,); //从位置0开始获取2个字节
这样,对获取的数据报进行全部转换:
String content = new String(dp.getData(),,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());//
/*测试字节数组*/
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());//
/*测试字节数组*/
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);即可搞定所有的,需要考虑的问题是编码问题
java对获取的字节数组进行处理的更多相关文章
- Java将文件转为字节数组
Java将文件转为字节数组 关键字:文件,文件流,字节流,字节数组,二进制 摘要:最近工作中碰到的需求是,利用http传输二进制数据到服务器对应接口,需要传输userId, file(加密后)等一系列 ...
- Java文件与io——字节数组流数据流字符串流
字节数组流 ByteArrayInputStream:包含一个内部缓冲区,该缓冲区包含从流中读取的字节.内部计数器跟踪read方法要提供的下一个字节.关闭ByteArrayInputStream无效. ...
- java 读取文件的字节数组
/*文件64位编码*/ public static void main(String[] args) { byte[] fileByte = toByteArray(newFile); St ...
- Java中文件与字节数组转换
注:来源于JavaEye 文件转化为字节数组: http://www.javaeye.com/topic/304980 /** * 文件转化为字节数组 * * @param file * @retur ...
- 【Java】获取二维数组行列长度
二维数组int array[][] = new int[3][3]; 行长度:array.length 列长度:array[i].length
- C#获取文件/字节数组MD5值方法
找了很多,就这个管用,有时间好好研究一番 public static string GetMD5Hash(string fileName) { try { FileStream file = new ...
- JAVA把InputStream 转 字节数组(byte[])
import org.apache.commons.io.IOUtils; byte[] bytes = IOUtils.toByteArray(inputStream); 如果没有这个包 就加下依赖 ...
- 【Java】字节数组转换工具类
import org.apache.commons.lang.ArrayUtils; import java.nio.charset.Charset; /** * 字节数组转换工具类 */ publi ...
- java对象转字节数组,获取泛型类
对象转字节数组,字节数组在恢复成对象 Test.java class Test { public static void main(String args[]) throws IOException, ...
随机推荐
- CodeForces 626C Block Towers
构造AC的.左右两边都先不用6的倍数,然后哪边数字大那一边往回退一下,然后再比较哪边数字大.......直到结束 #include<cstdio> #include<cstring& ...
- 3505: [Cqoi2014]数三角形
3505: [Cqoi2014]数三角形 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1324 Solved: 807[Submit][Statu ...
- 一种比较简单的在USB U盘中访问nandflash的方法
u8 nandflash_write_buffer[NAND_SERECT_FULL_SIZE]; static int currentBlock = -1; static int currentPa ...
- C#调用bat 不显示DOS窗口,禁止DOS窗口一闪而过
ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.CreateNoWindow = true;//不创建窗口
- MPU6050程序(转)
源:MPU6050程序 初始化定义 #ifndef _MPU6050_H #define _MPU6050_H #define PORT_USED 0 #define MPU6050_ADDRESS_ ...
- IOS开发-ObjC-对象、封装
C语言是基于过程的一种编程语言,而OC语言是基于对象的一种语言. C是和其他的面向对象的语言的区别在于C语言更注重地层操作,思维方式相比面向对象的语言而言更接近机器的思维方式,而面向对象的语言更接近于 ...
- Android与Linux以及GNU的关系
转帖自 http://www.eefocus.com/Kevin/blog/09-11/179409_1dc9a.html 作者: Kevin 本文转贴自 http://mmdays.com/2008 ...
- IE6中让png的icon图标也透明的完整代码段
一个引用了在IE6中让img图标也可引用png图片的js插件代码的html是这样写的,兼容IE6: <a class="btn btn-select" href=" ...
- Memo 的当前行、当前列与当前字符
procedure TForm1.Memo1Click(Sender: TObject); begin Text := Format('当前列:%d, 当前行:%d', [Memo1.CaretP ...
- win10新特性,ubuntu子系统(安装及配置)
最新版win10下可以直接跑ubuntu镜像,直接入正题. 这里如果你没有可能是你的版本不是最新的,我这里是最新的win10直接是有这个功能的.勾选后会要求重启,确定即可. 然后win键弹出搜索,输入 ...