如何从Java中的字节数组中获取数据?
Posted
技术标签:
【中文标题】如何从Java中的字节数组中获取数据?【英文标题】:How to get the data from the byte array in Java? 【发布时间】:2015-02-05 09:30:42 【问题描述】:我使用byte[512]
从 Java 中的套接字接收数据
我使用函数bytesToHexString
让它可见
public static String bytesToHexString(byte[] src)
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0)
return null;
for (int i = 0; i < src.length; i++)
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2)
stringBuilder.append(0);
stringBuilder.append(hv + " ");
return stringBuilder.toString();
它看起来像这样:
50 1c 04 80 00 00 ce 01 4d 6f 64 69 6d 20 66 61 73 68 69 6f 6e 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 64 33 36 30 57 69 46 69 2d 36 38 38 38 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 5a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 50 74 69 6d 65 6c 65 73 73 67 7a 32 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 4b 77 7a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 43 74 69 6d 65 6c 65 73 73 67 7a 31 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 39 72 69 6e 67 69 65 72 67 75 65 73 74 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 39 73 77 65 65 74 20 62 61 62 79 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 34 4a 49 4d 4d 59 20 33 47 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 28 42 65 72 74 2d 43 6f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 28 54 50 2d 4c 49 4e 4b 5f 43 42 44 30 34 43 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 25 43 68 69 6e 61 4e 65 74 2d 47 50 69 36 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 23 43 68 69 6e 61 4e 65 74 2d 55 6c 36 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 11 62 6c 75 65 31 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 11 95 bd 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
而我所知道的是 C 中的这个结构:
typedef struct _xchip_cmd_head
u16 flag; //always 50 1c
u16 cmd; // commands, return cmd=cmd|0x8000
u16 cmd_status; //return result
u16 datalen;
u8 data[1];
xchip_cmd_head_t;
50 1c
是开始标志
04 80
是命令
00 00
是一种状态
ce 01
是数据的长度。
之后ce 01
是ssid的列表:
typedef struct _ApList_str
char ssid[32];
char ApPower; // min:0, max:100
ApList_str;
那么如何在 Java 中获取 ssid 列表呢?
我做了一些搜索,有没有办法使用ByteBuffer
?
【问题讨论】:
有点不清楚你想要什么:你已经在Java端成功解析了字节数组,甚至将内容打印为十六进制字符串,你没有做些什么? @gio 我想要做的是拆分 ssid 的字符串,并将其放入List
【参考方案1】:
50 1c 是开始标志
04 80 是命令
00 00 是一种状态
ce 01 是 ssid 列表长度
如果上面的列表是常量,类似于应用程序协议缓冲区,那么您可以解析字节数组并打印值。 检查半径数据包的解析。
【讨论】:
那么如何解析呢?如何将此十六进制值转换为字符串? 可以通过此链接mdsaputra.wordpress.com/2010/09/03/… 将十六进制值转换为字符串,反之亦然【参考方案2】:你没有说ApPower
在哪里编码,否则这个类会做你想做的。
public class ApList_str
private final String ssid;
private int ApPower = -1; /* Unknown */
public ApList_str(String ssid)
this.ssid = ssid;
public String getSsid()
return ssid;
public int getApPower()
return ApPower;
public void setApPower(int apPower)
ApPower = apPower;
public static ApList_str fromBytes(ByteBuffer b) throws UnsupportedEncodingException
byte[] ssid_bytes = new byte[32];
b.get(ssid_bytes);
/* You should change the encoding depending on your situation */
return new ApList_str(new String(ssid_bytes, "UTF-8"));
【讨论】:
嗨,但我还不知道如何使用这个类。我得到的是一个byte[512]
以及如何拆分这个字节数组???
你可以使用 ByteBuffer.wrap() 吗?【参考方案3】:
最后我通过这个函数获得了ssid
列表。它看起来很愚蠢,但它确实有效。
我想找到更好的方法:
public static List<String> getSsidList(byte[] byteRecv)
System.out.println("get from device");
List<String> ssidListOpt = new ArrayList<String>();
try
if (byteRecv[0] != 0)
// make it visabled
String test = BytesUtil.bytesToHexString(byteRecv);
// replace the space
test = test.replace(" ", "");
// get rid of the head data
test = test.substring(16);
String temp = "";
// split the ssid,add "," between every ssid item
for (int i = 0; i < test.length(); i++)
if (i % 66 == 0 && i != 0)
temp += ",";
temp += test.substring(i, i + 1);
// put it into the string array
String temps[] = temp.split(",");
for (int i = 0; i < temps.length; i++)
if (temps[i].endsWith("00"))
continue;
int l = temps[i].length();
// get the hex ssid name
String ssidName = temps[i].substring(0, l - 2);
// delete all of 0x00 at the end
ssidName = BytesUtil.deleteLastZero(ssidName);
// hex ssid name to string
ssidName = BytesUtil.toStringHex(ssidName);
if ("".equals(ssidName))
continue;
// get the hex ApPower
String signal = temps[i].substring(l - 2, l);
// hex to int
signal = Integer.parseInt(signal, 16) + "";
String t[] = ssidName, signal;
System.out.println("" + t[0] + "------" + t[1]);
ssidListOpt.add(ssidName);
catch (Exception e)
e.printStackTrace();
return ssidListOpt;
这就是我在 LogCat 中得到的:
02-06 15:06:40.398: I/System.out(20947): Modim fashion------100
02-06 15:06:40.398: I/System.out(20947): tessgz2------80
02-06 15:06:40.398: I/System.out(20947): sweet baby------67
02-06 15:06:40.398: I/System.out(20947): wz------62
02-06 15:06:40.408: I/System.out(20947): Bert-Co------45
02-06 15:06:40.408: I/System.out(20947): ChinaNet-GPi6------40
02-06 15:06:40.408: I/System.out(20947): ringierguest------40
02-06 15:06:40.408: I/System.out(20947): tlessgz1------32
02-06 15:06:40.408: I/System.out(20947): JIMMY 3G------32
02-06 15:06:40.408: I/System.out(20947): blue1------30
02-06 15:06:40.408: I/System.out(20947): ChinaNet-Ul6------25
02-06 15:06:40.408: I/System.out(20947): wudiuandiuan------15
【讨论】:
以上是关于如何从Java中的字节数组中获取数据?的主要内容,如果未能解决你的问题,请参考以下文章