字符串的拆分与使用
虽然代码一直提倡简洁、可读性强,但大多数数据(特别是一些机器发送的数据包)都不是省油的灯儿:
//很多时候的数据包都是这种长得让人绝望的加密数据。。。。。。
String buffers = "0000803fc9022643000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000803fc9022643c90226430000803f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000004040000000000000000000000000000000000000000000000000000000000000000000000000000000000000804000000000000000000000000000000000000000000000000000000000000000000000000000000000c90226430000000000000000c90226430000000000000000c90226430000000000000000c90226430000000000000000c90226430000000000000000c90226430000000000000000c90226430000000000000000c90226430000000000000000c90226430000000000000000c90226430000000000000000c90226430000000000000000c9022643000000000000803f00000000000000000000000000000000000000000000000000000000000000000000000000000000";
先不考虑“为啥会有这么多零”,我们现在就来进行一个流程化的数据解析。
首先,很多的数据包是加密过的,因此需要进行解密,这里根据各自的工具、加密手段来进行解密即可;
然后就是你所获得的原始数据了,这里要注意一下原始数据的进位计数制,不同进制的数所占字节(byte,1byte = 8位(bit))数也不同。以下为常见进制所占字节数:
十进制:1字节/进制位
二进制:即bit,1字节=8bit
八进制:8/3个字节/进制位(很少使用)
十六进制:0.5字节/进制位 (常用)
这里要注意一下,常用的传递方式一般为十六进制,也就是说,两个数字对应一个字节,具体的算法、解决方案将记录在下一章,现在回过头来看自己的那一大串原始数据:
现在做个假设,你的数据包长度为1024byte,而数据采用的是十六进制,那么你在截取数据串的时候就要注意了,尽管你的包长度为1024byte,但是你所获得的字符串字符个数为1024的两倍,这是因为十六进制下,2个进制位才等于一个字节。
好了,准备工作做完,现在就进行字符串的截取吧。
方法一:
1、将字符串转变为char类型的字符型数组
char bufferArray[] = buffers;
这里要注意一下,和C语言不同,java的字符串和字符型数组是两个概念。
2、截取所需要的内容。
char eventArray[] = copyOfRange(buffersArray,2439 ,2727);
方法二:
直接使用substring方法截取。
String eventArray2 = buffers.substring(2439,2727);
如果只是需要字符串一段内容的话,方法二更简单,但是,绝大多数的数据都是需要详细拆分使用的,这里更推荐带有元素索引的方法一。