0

I use a byte[512] to recive the data from socket in Java

I use the function bytesToHexString make it visiable

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();
}

and it look like that:

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 

and what I known is this struct in 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 is start flag

04 80 is the command

00 00 is a status

ce 01 is the length of the data.

and afterce 01

is the list of ssid:

typedef  struct  _ApList_str  
{  
  char ssid[32];  
  char ApPower;  // min:0, max:100
}ApList_str; 

So how can I pick up the list of ssid in Java?

I do some search, is there any way use ByteBuffer?

2
  • it is a little bit unclear what you want: you already successfully parsed the byte array at Java side and even printed the contents as hex strings, what didn't you manage to do? Feb 5, 2015 at 11:05
  • @gio what I want to do is split the ssid's string, and put it to a List
    – chanjianyi
    Feb 6, 2015 at 2:38

3 Answers 3

0

50 1c is start flag

04 80 is the command

00 00 is a status

ce 01 is the ssid list length

If the above list is constant, as similar to application protocol buffers, then you can parse the bytearray and print the values. check for parsing of radius packet.

2
0

You don't say where ApPower is encoded, but otherwise this class will do what you want.

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"));
    }
}
2
  • Hi, but I dont know how to use the class yet. what I got is a byte[512] and how to split this byte array???
    – chanjianyi
    Feb 6, 2015 at 2:45
  • You could use ByteBuffer.wrap()?
    – robert
    Feb 6, 2015 at 6:49
0

Finally I picked up the ssid list by this function. It looks stupid, but it works.

I want to find the better way:

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;
}

that's what I got in the 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.