Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中从字节数组中获取数据?_Java_C_Struct_Bytearray_Bytebuffer - Fatal编程技术网

如何在Java中从字节数组中获取数据?

如何在Java中从字节数组中获取数据?,java,c,struct,bytearray,bytebuffer,Java,C,Struct,Bytearray,Bytebuffer,我使用字节[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

我使用
字节[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();
}
我知道的是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
是启动标志

0480
是命令

00
是一种状态

ce 01
是数据的长度

ce 01之后

是ssid的列表:

typedef  struct  _ApList_str  
{  
  char ssid[32];  
  char ApPower;  // min:0, max:100
}ApList_str; 
那么,如何在Java中获取ssid列表呢

我做了一些搜索,有没有办法使用
ByteBuffer

50 1c是开始标志

0480是命令

00是一种状态

ce 01是ssid列表长度

如果上面的列表是常量,就像应用程序协议缓冲区一样,那么您可以解析bytearray并打印值。
检查radius数据包的解析。

您不能说
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"));
    }
}

最后,我通过这个函数获取了
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;
}

那么如何解析它呢?如何将此十六进制值转换为字符串?可能是通过此链接将十六进制值转换为字符串,反之则有点不清楚您想要什么:您已经成功解析了Java端的字节数组,甚至将内容打印为十六进制字符串,您没有做什么?@gio我想做的是拆分ssid的字符串,然后把它放到
列表中
嗨,但是我还不知道如何使用这个类。我得到的是一个
字节[512]
以及如何拆分这个字节数组???您可以使用ByteBuffer.wrap()?
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;
}
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