Java Two-Dimensional Array Syntax Wrapping Borders - java
I am creating a two-dimensional array in Java that reads a number from the user and makes a grid that size. To start the array, I would like to place a 1 in the middle of the top row. We will call this K. After K has been placed, I would like to place K+1 to the right and up wrapping around the borders. However, if the position to the right and up has already been filled, OR it is in the upper right-hand corner, then you must move to the position straight down instead. Here is an example of a 7x7 grid that follows these steps.
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
I tried a couple of different things, and I was unable to come up with the correct solution. I have a feeling that divs and mods are used in this somehow but I can not figure it out. If someone would like a good challenge, this is it hahah!
I've written the code to YOUR specifications, AND shown what the grid looks like at each point in the process. It does not come out as your expected result...so either you haven't described the rules correctly, or you've got the "expected output" written incorrectly.
Here's my code:
import java.util.Scanner;
import java.text.DecimalFormat;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Size of grid? ");
int size = sc.nextInt();
if (size > 0) {
int[][] grid = new int[size][size];
int max = (int)Math.pow(size, 2);
int length = Integer.toString(max).length();
int col = size / 2;
int row = 0;
grid[row][col]=1;
displayGrid(grid, length, row, col);
for (int i=2; i<=max; i++) {
int newCol = (col+1) == size ? 0 : (col+1); // to the right, wrapping
int newRow = (row-1) >= 0 ? (row-1) : size-1; // up, wrapping
// if the new position is already taken or is the top right corner
if (grid[newRow][newCol]!=0 || (newRow==0 && newCol==(size-1))) {
// then move straight down, wrapping, instead
newRow = (row+1) < size ? (row+1) : 0;
newCol = col;
}
// put the current number in its spot and update row, col
grid[newRow][newCol]=i;
row = newRow;
col = newCol;
displayGrid(grid, length, row, col);
}
}
}
public static void displayGrid(int[][] grid, int padLength, int curRow, int curCol) {
String format = "";
for(int i=0; i<padLength; i++) {
format = format + "0";
}
DecimalFormat df = new DecimalFormat(format);
for(int r=0; r<grid.length; r++) {
for(int c=0; c<grid[r].length; c++) {
System.out.print(df.format(grid[r][c]));
System.out.print(r==curRow && c==curCol ? "*" : " ");
}
System.out.println();
}
System.out.println();
}
}
Resulting output: Asterisk, *, is to the right of the number just placed!
Size of grid? 7
00 00 00 01*00 00 00
00 00 00 00 00 00 00
00 00 00 00 00 00 00
00 00 00 00 00 00 00
00 00 00 00 00 00 00
00 00 00 00 00 00 00
00 00 00 00 00 00 00
00 00 00 01 00 00 00
00 00 00 00 00 00 00
00 00 00 00 00 00 00
00 00 00 00 00 00 00
00 00 00 00 00 00 00
00 00 00 00 00 00 00
00 00 00 00 02*00 00
00 00 00 01 00 00 00
00 00 00 00 00 00 00
00 00 00 00 00 00 00
00 00 00 00 00 00 00
00 00 00 00 00 00 00
00 00 00 00 00 03*00
00 00 00 00 02 00 00
00 00 00 01 00 00 00
00 00 00 00 00 00 00
00 00 00 00 00 00 00
00 00 00 00 00 00 00
00 00 00 00 00 00 04*
00 00 00 00 00 03 00
00 00 00 00 02 00 00
00 00 00 01 00 00 00
00 00 00 00 00 00 00
00 00 00 00 00 00 00
05*00 00 00 00 00 00
00 00 00 00 00 00 04
00 00 00 00 00 03 00
00 00 00 00 02 00 00
00 00 00 01 00 00 00
00 00 00 00 00 00 00
00 06*00 00 00 00 00
05 00 00 00 00 00 00
00 00 00 00 00 00 04
00 00 00 00 00 03 00
00 00 00 00 02 00 00
00 00 00 01 00 00 00
00 00 07*00 00 00 00
00 06 00 00 00 00 00
05 00 00 00 00 00 00
00 00 00 00 00 00 04
00 00 00 00 00 03 00
00 00 00 00 02 00 00
00 00 00 01 00 00 00
00 00 07 00 00 00 00
00 06 08*00 00 00 00
05 00 00 00 00 00 00
00 00 00 00 00 00 04
00 00 00 00 00 03 00
00 00 00 00 02 00 00
00 00 00 01 00 00 00
00 00 07 09*00 00 00
00 06 08 00 00 00 00
05 00 00 00 00 00 00
00 00 00 00 00 00 04
00 00 00 00 00 03 00
00 00 00 00 02 00 00
00 00 00 01 10*00 00
00 00 07 09 00 00 00
00 06 08 00 00 00 00
05 00 00 00 00 00 00
00 00 00 00 00 00 04
00 00 00 00 00 03 00
00 00 00 00 02 00 00
00 00 00 01 10 00 00
00 00 07 09 00 00 00
00 06 08 00 00 00 00
05 00 00 00 00 00 00
00 00 00 00 00 00 04
00 00 00 00 00 03 00
00 00 00 00 02 11*00
00 00 00 01 10 00 00
00 00 07 09 00 00 00
00 06 08 00 00 00 00
05 00 00 00 00 00 00
00 00 00 00 00 00 04
00 00 00 00 00 03 12*
00 00 00 00 02 11 00
00 00 00 01 10 00 00
00 00 07 09 00 00 00
00 06 08 00 00 00 00
05 00 00 00 00 00 00
13*00 00 00 00 00 04
00 00 00 00 00 03 12
00 00 00 00 02 11 00
00 00 00 01 10 00 00
00 00 07 09 00 00 00
00 06 08 00 00 00 00
05 14*00 00 00 00 00
13 00 00 00 00 00 04
00 00 00 00 00 03 12
00 00 00 00 02 11 00
00 00 00 01 10 00 00
00 00 07 09 00 00 00
00 06 08 00 00 00 00
05 14 00 00 00 00 00
13 15*00 00 00 00 04
00 00 00 00 00 03 12
00 00 00 00 02 11 00
00 00 00 01 10 00 00
00 00 07 09 00 00 00
00 06 08 00 00 00 00
05 14 16*00 00 00 00
13 15 00 00 00 00 04
00 00 00 00 00 03 12
00 00 00 00 02 11 00
00 00 00 01 10 00 00
00 00 07 09 00 00 00
00 06 08 17*00 00 00
05 14 16 00 00 00 00
13 15 00 00 00 00 04
00 00 00 00 00 03 12
00 00 00 00 02 11 00
00 00 00 01 10 00 00
00 00 07 09 18*00 00
00 06 08 17 00 00 00
05 14 16 00 00 00 00
13 15 00 00 00 00 04
00 00 00 00 00 03 12
00 00 00 00 02 11 00
00 00 00 01 10 19*00
00 00 07 09 18 00 00
00 06 08 17 00 00 00
05 14 16 00 00 00 00
13 15 00 00 00 00 04
00 00 00 00 00 03 12
00 00 00 00 02 11 00
00 00 00 01 10 19 00
00 00 07 09 18 00 00
00 06 08 17 00 00 00
05 14 16 00 00 00 00
13 15 00 00 00 00 04
00 00 00 00 00 03 12
00 00 00 00 02 11 20*
00 00 00 01 10 19 00
00 00 07 09 18 00 00
00 06 08 17 00 00 00
05 14 16 00 00 00 00
13 15 00 00 00 00 04
21*00 00 00 00 03 12
00 00 00 00 02 11 20
00 00 00 01 10 19 00
00 00 07 09 18 00 00
00 06 08 17 00 00 00
05 14 16 00 00 00 00
13 15 00 00 00 00 04
21 00 00 00 00 03 12
22*00 00 00 02 11 20
00 00 00 01 10 19 00
00 00 07 09 18 00 00
00 06 08 17 00 00 00
05 14 16 00 00 00 00
13 15 00 00 00 00 04
21 23*00 00 00 03 12
22 00 00 00 02 11 20
00 00 00 01 10 19 00
00 00 07 09 18 00 00
00 06 08 17 00 00 00
05 14 16 00 00 00 00
13 15 24*00 00 00 04
21 23 00 00 00 03 12
22 00 00 00 02 11 20
00 00 00 01 10 19 00
00 00 07 09 18 00 00
00 06 08 17 00 00 00
05 14 16 25*00 00 00
13 15 24 00 00 00 04
21 23 00 00 00 03 12
22 00 00 00 02 11 20
00 00 00 01 10 19 00
00 00 07 09 18 00 00
00 06 08 17 26*00 00
05 14 16 25 00 00 00
13 15 24 00 00 00 04
21 23 00 00 00 03 12
22 00 00 00 02 11 20
00 00 00 01 10 19 00
00 00 07 09 18 27*00
00 06 08 17 26 00 00
05 14 16 25 00 00 00
13 15 24 00 00 00 04
21 23 00 00 00 03 12
22 00 00 00 02 11 20
00 00 00 01 10 19 00
00 00 07 09 18 27 00
00 06 08 17 26 28*00
05 14 16 25 00 00 00
13 15 24 00 00 00 04
21 23 00 00 00 03 12
22 00 00 00 02 11 20
00 00 00 01 10 19 00
00 00 07 09 18 27 29*
00 06 08 17 26 28 00
05 14 16 25 00 00 00
13 15 24 00 00 00 04
21 23 00 00 00 03 12
22 00 00 00 02 11 20
30*00 00 01 10 19 00
00 00 07 09 18 27 29
00 06 08 17 26 28 00
05 14 16 25 00 00 00
13 15 24 00 00 00 04
21 23 00 00 00 03 12
22 00 00 00 02 11 20
30 00 00 01 10 19 00
00 00 07 09 18 27 29
00 06 08 17 26 28 00
05 14 16 25 00 00 00
13 15 24 00 00 00 04
21 23 00 00 00 03 12
22 31*00 00 02 11 20
30 00 00 01 10 19 00
00 00 07 09 18 27 29
00 06 08 17 26 28 00
05 14 16 25 00 00 00
13 15 24 00 00 00 04
21 23 32*00 00 03 12
22 31 00 00 02 11 20
30 00 00 01 10 19 00
00 00 07 09 18 27 29
00 06 08 17 26 28 00
05 14 16 25 00 00 00
13 15 24 33*00 00 04
21 23 32 00 00 03 12
22 31 00 00 02 11 20
30 00 00 01 10 19 00
00 00 07 09 18 27 29
00 06 08 17 26 28 00
05 14 16 25 34*00 00
13 15 24 33 00 00 04
21 23 32 00 00 03 12
22 31 00 00 02 11 20
30 00 00 01 10 19 00
00 00 07 09 18 27 29
00 06 08 17 26 28 00
05 14 16 25 34 00 00
13 15 24 33 35*00 04
21 23 32 00 00 03 12
22 31 00 00 02 11 20
30 00 00 01 10 19 00
00 00 07 09 18 27 29
00 06 08 17 26 28 00
05 14 16 25 34 36*00
13 15 24 33 35 00 04
21 23 32 00 00 03 12
22 31 00 00 02 11 20
30 00 00 01 10 19 00
00 00 07 09 18 27 29
00 06 08 17 26 28 37*
05 14 16 25 34 36 00
13 15 24 33 35 00 04
21 23 32 00 00 03 12
22 31 00 00 02 11 20
30 00 00 01 10 19 00
38*00 07 09 18 27 29
00 06 08 17 26 28 37
05 14 16 25 34 36 00
13 15 24 33 35 00 04
21 23 32 00 00 03 12
22 31 00 00 02 11 20
30 39*00 01 10 19 00
38 00 07 09 18 27 29
00 06 08 17 26 28 37
05 14 16 25 34 36 00
13 15 24 33 35 00 04
21 23 32 00 00 03 12
22 31 00 00 02 11 20
30 39 00 01 10 19 00
38 00 07 09 18 27 29
00 06 08 17 26 28 37
05 14 16 25 34 36 00
13 15 24 33 35 00 04
21 23 32 00 00 03 12
22 31 40*00 02 11 20
30 39 00 01 10 19 00
38 00 07 09 18 27 29
00 06 08 17 26 28 37
05 14 16 25 34 36 00
13 15 24 33 35 00 04
21 23 32 41*00 03 12
22 31 40 00 02 11 20
30 39 00 01 10 19 00
38 00 07 09 18 27 29
00 06 08 17 26 28 37
05 14 16 25 34 36 00
13 15 24 33 35 00 04
21 23 32 41 00 03 12
22 31 40 42*02 11 20
30 39 00 01 10 19 00
38 00 07 09 18 27 29
00 06 08 17 26 28 37
05 14 16 25 34 36 00
13 15 24 33 35 00 04
21 23 32 41 43*03 12
22 31 40 42 02 11 20
30 39 00 01 10 19 00
38 00 07 09 18 27 29
00 06 08 17 26 28 37
05 14 16 25 34 36 00
13 15 24 33 35 44*04
21 23 32 41 43 03 12
22 31 40 42 02 11 20
30 39 00 01 10 19 00
38 00 07 09 18 27 29
00 06 08 17 26 28 37
05 14 16 25 34 36 45*
13 15 24 33 35 44 04
21 23 32 41 43 03 12
22 31 40 42 02 11 20
30 39 00 01 10 19 00
38 00 07 09 18 27 29
46*06 08 17 26 28 37
05 14 16 25 34 36 45
13 15 24 33 35 44 04
21 23 32 41 43 03 12
22 31 40 42 02 11 20
30 39 00 01 10 19 00
38 47*07 09 18 27 29
46 06 08 17 26 28 37
05 14 16 25 34 36 45
13 15 24 33 35 44 04
21 23 32 41 43 03 12
22 31 40 42 02 11 20
30 39 48*01 10 19 00
38 47 07 09 18 27 29
46 06 08 17 26 28 37
05 14 16 25 34 36 45
13 15 24 33 35 44 04
21 23 32 41 43 03 12
22 31 40 42 02 11 20
30 39 48 01 10 19 00
38 47 49*09 18 27 29
46 06 08 17 26 28 37
05 14 16 25 34 36 45
13 15 24 33 35 44 04
21 23 32 41 43 03 12
22 31 40 42 02 11 20
Expected Output from Author's post:
30 39 48 01 10 19 28
38 47 07 09 18 27 29
46 06 08 17 26 35 37
05 14 16 25 34 36 45
13 15 24 33 42 44 04
21 23 32 41 43 03 12
22 31 40 49 02 11 20
Related
Data conversion from byte array to String
I'm trying to convert Byte Array to String with Following code snippet. But for some reasons, when i convert the byte[] to string, it changes the some content in in file Code public String convertToString(byte[] byteArr) { public static final int BYTE_MASK = 0xFF; StringBuilder strBldr = new StringBuilder(); for(int i = 0; i < byteArr.length; i++ ) { strBldr.append((char) (byteArr[i] & BYTE_MASK)); } return strBldr.toString(); } I have added the data of two files called expected file and generated file Expected File: 00 39 00 00 46 91 00 00 00 17 16 02 16 16 39 31 0b 00 3a 00 78 09 60 40 26 64 50 41 50 20 48 49 47 20 52 4d 20 20 04 00 80 4b 02 00 a0 ea 01 00 64 00 ec 05 00 00 00 00 00 Generated File: 00 39 00 00 46 3f 00 00 00 17 16 02 16 16 39 31 0b 00 3a 00 78 09 60 40 26 64 50 41 50 20 48 49 47 20 52 4d 20 20 04 00 3f 4b 02 00 a0 ea 01 00 64 00 ec 05 00 00 00 00 00 if you see both files, then expected file data should be "91"(First row,sixth element) and its 3f in generated file. Any idea how would get a correct output?
try this: String example = "This is an example"; byte[] bytes = example.getBytes(); System.out.println("Text : " + example); String s = new String(bytes); System.out.println("String : " + s); output: Text : [B#187aeca String : This is an example
Java = Building packets from scratch with Jnetpcap
I'm trying to build a packet using jnetpcap, according to the packets I receive. I'm currently using jnetpcap-1.4.r1425, and am running into several problems. The following code is common to all my problems: JPacket wPacket = new JMemoryPacket(size); wPacket.order(ByteOrder.BIG_ENDIAN); wPacket.setUShort(12, 0x0800); wPacket.scan(JProtocol.ETHERNET_ID); wEth = wPacket.getHeader(new Ethernet()); wEth.source(sourceMac); wEth.destination(destMac); wEth.checksum(wEth.calculateChecksum()); headersize = 14; wPacket.setUByte(14, 0x40 | 0x05); wPacket.scan(JProtocol.ETHERNET_ID); wIp4 = wPacket.getHeader(new Ip4()); if (packet.hasHeader(Icmp.ID)) wIp4.type(Ip4.Ip4Type.ICMP); else if (packet.hasHeader(Tcp.ID)) wIp4.type(Ip4.Ip4Type.TCP); else if (packet.hasHeader(Udp.ID)) wIp4.type(Ip4.Ip4Type.UDP); wIp4.length(500 - wEth.size()); wIp4.source(dIP); wIp4.destination(sIP); wIp4.ttl(32); wIp4.flags(0); wIp4.offset(0); wIp4.checksum(wIp4.calculateChecksum()); headersize += 20; Attempting to get TCP header from above packet gives me null After the above portion of code, I check the original packet for UDP/TCP/ICMP headers. The portion for TCP reads as follows: if (packet.hasHeader(Tcp.ID)) { wPacket.scan(JProtocol.ETHERNET_ID); wTcp = wPacket.getHeader(new Tcp()); ...... } This is the same thing I do for the UDP portion, but the TCP portion errors out. It turns out wTcp == null, and I have no idea why it comes out this way. ICMP checksum is wrong The check sums of the common portion work out fine. However, the ICMP checksum always comes out as being incorrect. This is what I'm doing for ICMP: else if (packet.hasHeader(icmp) && icmp.hasSubHeader((new Icmp.EchoRequest()))) { wPacket.scan(JProtocol.ETHERNET_ID); wIcmp = wPacket.getHeader(new Icmp()); wIcmp.setUByte(0, 0); wIcmp.setUByte(1, 0); JHeader[] tmp = {new Icmp.EchoReply()}; wIcmp.setSubHeaders(tmp); wIcmp.checksum(wIcmp.calculateChecksum()); headersize += 8 + 4; } If anyone can point out what I might be doing wrong, I would much appreciated. Edit: toDebugString() and hexdump() after the scan in the above TCP section. I figure whatever the problem, it can be found here, JMemory: JMemory#7fd8a0c932d8class org.jnetpcap.packet.JPacket$State: size=304 bytes JMemory: owner=packet.JScanner.class(size=631888/offset=529976) JPacket.State#002: sizeof(packet_state_t)=184 JPacket.State#002: sizeof(header_t)=40 and *3=120 JPacket.State#002: pkt_header_map[0]=0x0000000000000007 JPacket.State#002: pkt_header_map[1]=0x0000000000000000 JPacket.State#002: pkt_header_map[2]=0x0000000000000000 JPacket.State#002: pkt_header_map[3]=0x0000000000000000 JPacket.State#002: pkt_flags=0x00000000 JPacket.State#002: pkt_header_count=3 JPacket.State#002: pkt_wirelen=500 bytes JPacket.State#002: pkt_caplen=500 bytes JPacket.State#002 : [ Protocol(ID/Flag) | Start | Prefix | Header | Gap | Payload | Postfix ] JPacket.State#002[0]: [ ETHERNET( 1/0800) | 0 | 0 | 14 | 0 | 486 | 0 ] JPacket.State#002[1]: [ IP4( 2/0800) | 14 | 0 | 20 | 0 | 466 | 0 ] JPacket.State#002[2]: [ PAYLOAD( 0/0800) | 34 | 0 | 466 | 0 | 0 | 0 ] 0000:*68 01 00 5e 00 00 01 5f 93 48 01 04 08 00*45 00 h..^..._.H....E. 0010: 01 e6 00 00 00 00 20 06 1d 37 2d 37 80 71 3d f0 ...... ..7-7.q=. 0020: 90 43*13 c0 d8 7f 00 00 01 00 00 00 d8 7f 00 00 .C.............. 0030: 02 00 00 00 74 2f 73 74 88 1e 13 c0 d8 7f 00 00 ....t/st........ 0040: 00 00 00 00 6c 64 3b 3e 00 00 00 00 00 00 00 00 ....ld;>........ 0050: 03 00 01 00 00 00 00 00 b5 00 00 00 00 00 00 00 ................ 0060: 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0070: 03 00 00 00 74 70 63 61 e0 1e 13 c0 d8 7f 00 00 ....tpca........ 0080: 02 00 00 00 d8 7f 00 00 02 00 00 00 72 6d 61 74 ............rmat 0090: 88 1e 13 c0 d8 7f 00 00 00 00 00 00 75 6d 56 61 ............umVa 00a0: 00 00 00 00 00 00 00 00 03 00 01 00 00 00 00 00 ................ 00b0: 19 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00c0: 00 00 00 00 00 00 00 00 00 00 00 00 49 47 01 00 ............IG.. 00d0: 00 00 00 00 00 00 00 00 00 00 00 00 d8 7f 00 00 ................ 00e0: 02 00 00 00 6f 42 79 74 88 1e 13 c0 d8 7f 00 00 ....oByt........ 00f0: 00 00 00 00 65 03 00 00 00 00 00 00 00 00 00 00 ....e........... 0100: 03 00 01 00 29 56 01 00 b8 00 00 00 00 00 00 00 ....)V.......... 0110: 04 00 00 00 00 00 00 00 09 00 0b 00 00 00 00 00 ................ 0120: 09 00 06 00 09 00 07 00 f0 1e 13 c0 d8 7f 00 00 ................ 0130: 01 00 00 00 00 00 00 00 02 00 00 00 6f 6c 2f 6c ............ol/l 0140: 88 1e 13 c0 d8 7f 00 00 00 00 00 00 45 74 68 65 ............Ethe 0150: 00 00 00 00 00 00 00 00 03 00 01 00 4c 6f 72 67 ............Lorg 0160: b5 00 00 00 d8 7f 00 00 05 00 00 00 00 00 00 00 ................ 0170: 00 00 00 00 00 00 00 00 06 00 00 00 61 64 65 72 ............ader 0180: 00 1f 13 c0 d8 7f 00 00 03 00 00 00 72 67 2f 6a ...........rg/j 0190: 02 00 00 00 63 61 70 2f 88 1e 13 c0 d8 7f 00 00 ....cap/........ 01a0: 00 00 00 00 4e 31 30 4d 00 00 00 00 00 00 00 00 ....N10M........ 01b0: 03 00 01 00 75 69 74 65 b1 00 00 00 6c 72 67 2f ....uite....lrg/ 01c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 01d0: 61 6e 6e 6f 74 61 74 65 00 00 00 00 00 00 00 00 annotate........ 01e0: 00 00 00 00 74 65 3b 01 02 00 00 00 4e 01 00 03 ....te;.....N... 01f0: 1d 31 50 a2* .1P. And here are the same for the icmp packet at the end. JMemory: JMemory#7fd8a0c94588class org.jnetpcap.packet.JPacket$State: size=344 bytes JMemory: owner=packet.JScanner.class(size=631888/offset=534760) JPacket.State#018: sizeof(packet_state_t)=184 JPacket.State#018: sizeof(header_t)=40 and *4=160 JPacket.State#018: pkt_header_map[0]=0x0000000000001007 JPacket.State#018: pkt_header_map[1]=0x0000000000000000 JPacket.State#018: pkt_header_map[2]=0x0000000000000000 JPacket.State#018: pkt_header_map[3]=0x0000000000000000 JPacket.State#018: pkt_flags=0x00000000 JPacket.State#018: pkt_header_count=4 JPacket.State#018: pkt_wirelen=500 bytes JPacket.State#018: pkt_caplen=500 bytes JPacket.State#018 : [ Protocol(ID/Flag) | Start | Prefix | Header | Gap | Payload | Postfix ] JPacket.State#018[0]: [ ETHERNET( 1/0800) | 0 | 0 | 14 | 0 | 486 | 0 ] JPacket.State#018[1]: [ IP4( 2/0800) | 14 | 0 | 20 | 0 | 466 | 0 ] JPacket.State#018[2]: [ ICMP(12/0800) | 34 | 0 | 8 | 0 | 458 | 0 ] JPacket.State#018[3]: [ PAYLOAD( 0/0800) | 42 | 0 | 458 | 0 | 0 | 0 ] 0000:*68 01 00 5e 00 00 01 5f 93 48 01 04 08 00*45 00 h..^..._.H....E. 0010: 01 e6 00 00 00 00 20 01 64 76 2d 37 80 71 58 c6 ...... .dv-7.qX. 0020: 2e 33*00 00 70 ed 00 00 00 00*00 00 00 00 53 87 .3..p.........S. 0030: 71 42 a6 f7 ad 57 f2 54 11 00 3e 61 ca 6e 49 b0 qB...W.T..>a.nI. 0040: a7 f7 59 4d b7 42 5e 05 0a f4 65 f9 5a 25 46 98 ..YM.B^...e.Z%F. 0050: dc ed 5a d0 2f a7 39 4d 02 73 bd c9 9f 11 bc ab ..Z./.9M.s...... 0060: d2 70 34 9b d0 be 15 9c cc 88 3e 9e 4f f4 aa e3 .p4.......>.O... 0070: 98 b5 14 88 e8 2e f5 8b 16 f5 be 47 a1 e2 31 f9 ...........G..1. 0080: 82 68 d3 1e d1 5d fb d0 b9 7f 95 63 0d f6 67 be .h...].....c..g. 0090: 38 df 34 df b5 48 65 f0 ff 79 82 79 72 6d 6b b0 8.4..He..y.yrmk. 00a0: 21 ee 69 6b 0f 0a ba f2 f5 52 d0 93 c2 66 b9 16 !.ik.....R...f.. 00b0: 2f 67 dc 56 48 27 40 85 0c 4d b4 b5 2f 81 bf 43 /g.VH'#..M../..C 00c0: 58 f5 2b 9d 19 87 b0 3e 14 a6 f9 d3 3e f7 b0 ca X.+....>....>... 00d0: 41 b1 73 05 89 6e 2c fb 6c 57 de 33 7f 5c 97 be A.s..n,.lW.3.\.. 00e0: c1 d9 d0 f5 53 60 70 83 3a a6 3e 54 1e 8e ae 4e ....S`p.:.>T...N 00f0: 80 4b 24 7b 35 4e 48 f3 df 29 f1 c4 6e 5e f0 53 .K${5NH..)..n^.S 0100: 15 f7 18 89 1e 4d 41 93 26 8d 72 90 fa 83 ec a3 .....MA.&.r..... 0110: f8 d1 60 b6 63 49 a3 19 8c e3 c1 1b 20 b3 db 3c ..`.cI...... ..< 0120: a4 cd 42 f9 53 ee 9d 86 7f f3 8b a0 c2 ff f4 9e ..B.S........... 0130: a4 ce 58 28 e0 b9 c2 0f 13 bf 87 a5 85 91 ff e6 ..X(............ 0140: 44 8c 93 43 33 b1 b4 ce 1c 02 22 19 9d da 28 7e D..C3....."...(~ 0150: 49 a6 84 c9 97 4a 1e 27 27 66 5d f2 b1 45 15 73 I....J.''f]..E.s 0160: 05 22 d6 9c 06 54 e7 17 2d 1d f6 7b f5 3c 3a d0 ."...T..-..{.<:. 0170: 43 40 1e 3e 80 18 a3 eb 2d 2b c5 ad 7b 71 6b 83 C#.>....-+..{qk. 0180: 3f 2b d7 5d 19 01 4d fc 66 57 69 38 97 f8 b2 aa ?+.]..M.fWi8.... 0190: 79 1c a7 0a b0 e5 33 7e b9 ef 23 27 78 a3 62 5a y.....3~..#'x.bZ 01a0: e7 26 e7 ff ce 34 31 86 ad d6 01 3c f8 7d 6c 30 .&...41....<.}l0 01b0: ed f7 62 d8 98 ce 21 bf e2 cd 3e 6a 8e a2 f0 48 ..b...!...>j...H 01c0: 30 62 5b 29 ed ab 91 82 d2 56 e8 12 15 2d 17 68 0b[).....V...-.h 01d0: a9 f5 d4 c9 95 3d b7 48 ce 91 c3 e7 a4 92 1f 6d .....=.H......m 01e0: e0 f1 0d ca 6a 67 a1 a3 60 22 6d 98 80 ac b7 62 ....jg..`"m....b 01f0: 03 7f 4a df*
My suggestion is to read and understand the code that is written by the author of jnetpcap. Specifically the code that reads the data from a pcap file and creates a packet from scratch. Basically how the peering works.
SELECT APDU doesn't work fine
I create a package(AID=0102030405) with two applets inside. This is the program of first applet (AID=01020304050202020202) that store contents of bArray to a static filed and returns it in process method : package processMethodArguments; import javacard.framework.APDU; import javacard.framework.Applet; import javacard.framework.ISOException; import javacard.framework.Util; public class ArgumentReturner extends Applet { //this is my static field that I want store bArray (the install method argument) in it. public static byte[] theArray={(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff}; public static short arrayLength=(short)0xFF; private ArgumentReturner() { } public static void install(byte bArray[], short bOffset, byte bLength) throws ISOException { new ArgumentReturner().register(); //Storing bArray in theArray. ArgumentReturner.arrayLength=(short)bArray.length; Util.arrayCopyNonAtomic(bArray, (short)0,ArgumentReturner.theArray , (short) 0, ArgumentReturner.arrayLength); } public void process(APDU apdu) throws ISOException { //returning theArray (=bArray) in response of any command. byte[] buffer=apdu.getBuffer(); Util.arrayCopyNonAtomic(ArgumentReturner.theArray, (short)0,buffer , (short) 0, ArgumentReturner.arrayLength); apdu.setOutgoingAndSend((short)0, (short)255); } } And this is the program of second applet (AID=010203040502) that do nothing : package processMethodArguments; import javacard.framework.APDU; import javacard.framework.Applet; import javacard.framework.ISOException; public class SecondApplet extends Applet { private SecondApplet() { } public static void install(byte bArray[], short bOffset, byte bLength) throws ISOException { new SecondApplet().register(); } public void process(APDU arg0) throws ISOException { // Nothing to do. So in reception of any command, it must return only 0x9000 } } I converted and installed this package on my card successfully : GP: gp -install D:\bArrayAccessibility\processMethodArguments.cap GP: gp -list AID: A000000151000000 (|....Q...|) ISD OP_READY: Security Domain, Card lock, Card terminate, Default selected, CVM (PIN) management AID: 01020304050202020202 (|..........|) App SELECTABLE: (none) AID: A0000001515350 (|....QSP|) ExM LOADED: (none) A000000151535041 (|....QSPA|) AID: 0102030405 (|.....|) ExM LOADED: (none) 01020304050202020202 (|..........|) 010203040502 (|......|) GP: gp -create 010203040502 -package 0102030405 -applet 010203040502 GP: gp -list AID: A000000151000000 (|....Q...|) ISD OP_READY: Security Domain, Card lock, Card terminate, Default selected, CVM (PIN) management AID: 01020304050202020202 (|..........|) App SELECTABLE: (none) AID: 010203040502 (|......|) App SELECTABLE: (none) AID: A0000001515350 (|....QSP|) ExM LOADED: (none) A000000151535041 (|....QSPA|) AID: 0102030405 (|.....|) ExM LOADED: (none) 01020304050202020202 (|..........|) 010203040502 (|......|) GP: Now, The problem is that: when I select first applet or second applet, both returns the APDU buffer (=theArray) : OpenSC: opensc-tool -s 00a404000a01020304050202020202 Using reader with a card: ACS CCID USB Reader 0 Sending: 00 A4 04 00 0A 01 02 03 04 05 02 02 02 02 02 Received (SW1=0x90, SW2=0x00): 0A 01 02 03 04 05 02 02 02 02 02 01 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ............... OpenSC: opensc-tool -s 00a4040006010203040502 Using reader with a card: ACS CCID USB Reader 0 Sending: 00 A4 04 00 06 01 02 03 04 05 02 Received (SW1=0x90, SW2=0x00): 0A 01 02 03 04 05 02 02 02 02 02 01 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ............... OpenSC: Q: Why it behave in this way? Why for both commands, the first applet seleted?
AID's are hierarchical. The Java Card runtime simply chooses the first one that matches against the bytes given, even if the AID contains more bytes. It may select the second one if you fire the SELECT again. To resolve this give the Applets AID's that are distinct even for the bytes in the shortest AID.
How to get the data from the byte array in Java?
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?
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.
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")); } }
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
Split a hex file to another files according to a condition
I have a hex file have values like f0 01 05 00 00 00 00 00 00 3d 61 c1 c1 df 40 02 00 c0 00 00 09 18 16 00 ef 01 40 00 00 3f 20 f0 01 03 00 00 00 00 00 00 c7 a2 c1 c1 df 40 02 00 c0 01 00 09 18 16 00 ef 01 40 00 00 3f 20 f0 01 02 00 00 00 00 00 00 50 e4 c1 c1 df 40 02 00 c0 02 00 09 79 09 04 c7 01 e1 4e 02 3f 00 f0 01 01 00 00 00 00 00 00 d9 25 c2 c1 df 40 02 00 c0 03 00 09 02 19 25 3c 00 22 6a 01 db 09 f0 04 05 00 00 00 00 00 00 62 67 c2 c1 df 40 02 00 c0 04 00 09 02 19 25 3c 00 22 6a 01 db 09 f0 01 07 00 00 00 00 00 00 ec a8 c2 c1 df 40 02 00 c0 05 00 13 7b 06 04 a4 0d 91 0c b6 c4 4e 02 3f 00 01 00 00 00 00 00 00 f0 01 08 00 00 00 00 00 00 6b 13 c3 c1 df 40 02 00 c0 06 00 13 7b 2b 04 93 0d 43 13 b6 c4 2e 00 3f 00 01 00 00 00 00 00 00 f0 01 09 00 00 00 00 00 00 e9 7d c3 c1 df 40 02 00 c0 07 00 13 7b 4f 04 d6 0d 2e fc b5 c4 2e 00 3f 00 01 00 00 00 00 00 00 f0 01 05 00 00 00 00 00 00 68 e8 c3 c1 df 40 02 00 c0 08 00 13 7b 55 04 ca 0d 7b 10 b6 c4 29 00 3f 00 01 00 00 00 00 00 00 f0 01 06 00 00 00 00 00 00 e7 52 c4 c1 df 40 02 00 c0 09 00 13 7b 46 04 f1 0d c5 15 b6 c4 2e 00 3f 00 01 00 00 00 00 00 00 I want whenever I find f0 followed by 01 followed by 01 to copy these pattern and whatever after it till the next f0 and so on till I have all the patterns that have these sequence and save it in a file and whenever I find f0 followed by 01 followed by 02 to copy these pattern and whatever after it till the next f0 and so on till I have all the patterns that have these sequence and save it in another file and so on with f0 01 03 till f0 01 09 Here is what I tried to do String filename = "C:\\tm09888.123"; FileInputStream in = null; int readHexFile = 0; char hexToChar = ' '; String[] bytes = new String[10]; try { in = new FileInputStream(filename); while((readHexFile = in.read()) != -1) { if (Integer.toHexString(readHexFile).equals("f0")) { System.out.print("\n\n\n"); } System.out.print(Integer.toHexString(readHexFile) + " "); } } catch (IOException ex) { Logger.getLogger(NARSSTest.class.getName()).log(Level.SEVERE, null, ex); } } I split all the pattern from f0 but how could I tell the program to look after f0 to be followed by 01 01 and then save all these patterns in another file