Data conversion from byte array to String - java

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

Related

Java Two-Dimensional Array Syntax Wrapping Borders

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

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.

JNI - SetByteArrayRegion does not work

I had some problems about encoding/decoding audio in my wrapper code, I debugged my code and finally found out that SetByteArrayRegion does not work as expected. Here we go:
// incoming parameters => jbyteArray jencoded
jbyte encoded[320]; //
... // fill encoded with 'encodedSize' number of bytes
int i;
for (i = 0; i < encodedSize; i++) {
fprintf(encodeDbg, "%02X ", (unsigned char)(encoded[i]));
}
fprintf(encodeDbg, "\n");
fflush(encodeDbg);
(*env)->SetByteArrayRegion(env, jencoded, 0, encodedSize, encoded);
jbyte* encoded_test = (*env)->GetByteArrayElements(env, jencoded, NULL);
for (i = 0; i < encodedSize; i++) {
fprintf(encodeDbg, "%02X ", (unsigned char)(encoded_test[i]));
}
fprintf(encodeDbg, "\n");
fflush(encodeDbg);
(*env)->ReleaseByteArrayElements(env, jencoded, encoded_test, JNI_ABORT);
The values in encoded and jencoded (i.e. encoded_test) do not match! After calling this procedure multiple times, the content of encodeDbg file is as follows:
08 40 64 6A 6A 00 00 00 61 16 0D 2B 2D 7C F1 EF 8B D0 AE D3 21 39
D0 F4 40 1B 00 00 00 00 16 00 00 00 00 F3 40 1B 8B D0 AE D3 21 39
08 40 64 6A 6A 00 00 00 74 DB F7 02 23 18 01 86 8E 6D 4A 56 9E 9C 64 41 43 E8 09
D0 F4 40 1B 00 00 00 00 1B 00 00 00 00 F3 40 1B 8E 6D 4A 56 9E 9C 64 41 43 E8 09
08 40 64 6A 6A 00 00 00 D5 A4 A3 5E A3 DC B1 3B 2D 3D 6C 4C A4
D0 F4 40 1B 00 00 00 00 15 00 00 00 00 F3 40 1B 2D 3D 6C 4C A4
08 40 64 6A 6A 00 00 00 AC 59 7A 0E 3D A1 50 43 57 20 58 41 5F 5C 74 88 22 1B 5D
D0 F4 40 1B 00 00 00 00 1B 00 00 00 00 F3 40 1B 57 20 58 41 5F 5C 74 88 22 1B 5D
08 40 64 6A 6A 00 00 00 73 33 13 51 56 77 BF 41 93 0A 61 9C 57 30 A3 DB B9 80
D0 F4 40 1B 00 00 00 00 1A 00 00 00 00 F3 40 1B 93 0A 61 9C 57 30 A3 DB B9 80
08 40 64 6A 6A 00 00 00 9C 05 BB 57 32 9B 14 2B 9E A1 87 DF EF 55 7F 4E B5 FD C2
D0 F4 40 1B 00 00 00 00 1B 00 00 00 00 F3 40 1B 9E A1 87 DF EF 55 7F 4E B5 FD C2
08 40 64 6A 6A 00 00 00 03 4C 7F ED 08 28 18 5D E1 23 D9 7C DD E8 27 BC 46 12 FA 0D 0E 56 29 C2 14 58
D0 F4 40 1B 00 00 00 00 22 00 00 00 00 F3 40 1B E1 23 D9 7C DD E8 27 BC 46 12 FA 0D 0E 56 29 C2 14 58
08 40 64 6A 6A 00 00 00 77 09 3F 7E 57 78 53 64 E2 39 74 6B E1 E5 AB 2C EB C4 D7 FE 80
D0 F4 40 1B 00 00 00 00 1D 00 00 00 00 F3 40 1B E2 39 74 6B E1 E5 AB 2C EB C4 D7 FE 80
There is a problem with the first 16 bytes, while the rest is of the array is ok. Any suggestion is highly appreciated.
EDIT:
I compile the JNI part (C stuff) on linux (with mingw), and copy the dll to Windows, where I run the Java application. May this cross platform operations cause the problem? Do I need to add some extra flag/parameter to the compiler or linker?
SECOND EDIT:
I slightly changed the code to see the contents of encoded after SetByteArrayRegion call. The code and the output is as following:
// incoming parameters => jbyteArray jencoded
jbyte encoded[320]; //
... // fill encoded with 'encodedSize' number of bytes
int i;
for (i = 0; i < encodedSize; i++) {
fprintf(encodeDbg, "%02X ", (unsigned char)(encoded[i]));
}
fprintf(encodeDbg, "\n");
fflush(encodeDbg);
(*env)->SetByteArrayRegion(env, jencoded, 0, encodedSize, encoded);
jbyte* encoded_test = (*env)->GetByteArrayElements(env, jencoded, NULL);
for (i = 0; i < encodedSize; i++) {
fprintf(encodeDbg, "%02X ", (unsigned char)(encoded_test[i]));
}
fprintf(encodeDbg, "\n");
for (i = 0; i < encodedSize; i++) {
fprintf(encodeDbg, "%02X ", (unsigned char)(encoded[i]));
}
fprintf(encodeDbg, "\n\n");
fflush(encodeDbg);
(*env)->ReleaseByteArrayElements(env, jencoded, encoded_test, JNI_ABORT);
Sample output:
08 40 64 6A 6A 00 00 00 61 16 0D 2B 2D 7C F1 EF 8B D0 AE D3 21 39
20 F1 07 1B 00 00 00 00 16 00 00 00 50 EF 07 1B 8B D0 AE D3 21 39
20 F1 07 1B 5C F2 07 1B 20 72 F1 76 AC BF CA B0 FE FF FF FF F6 2C
08 40 64 6A 6A 00 00 00 74 DB F7 02 23 18 01 86 8E 6D 4A 56 9E 9C 64 41 43 E8 09
20 F1 07 1B 00 00 00 00 1B 00 00 00 50 EF 07 1B 8E 6D 4A 56 9E 9C 64 41 43 E8 09
20 F1 07 1B 5C F2 07 1B 20 72 F1 76 AC BF CA B0 FE FF FF FF F6 2C F0 76 B0 18 64
08 40 64 6A 6A 00 00 00 D5 A4 A3 5E A3 DC B1 3B 2D 3D 6C 4C A4
20 F1 07 1B 00 00 00 00 15 00 00 00 50 EF 07 1B 2D 3D 6C 4C A4
20 F1 07 1B 5C F2 07 1B 20 72 F1 76 AC BF CA B0 FE FF FF FF F6
08 40 64 6A 6A 00 00 00 AC 59 7A 0E 3D A1 50 43 57 20 58 41 5F 5C 74 88 22 1B 5D
20 F1 07 1B 00 00 00 00 1B 00 00 00 50 EF 07 1B 57 20 58 41 5F 5C 74 88 22 1B 5D
20 F1 07 1B 5C F2 07 1B 20 72 F1 76 AC BF CA B0 FE FF FF FF F6 2C F0 76 B0 18 64
08 40 64 6A 6A 00 00 00 73 33 13 51 56 77 BF 41 93 0A 61 9C 57 30 A3 DB B9 80
20 F1 07 1B 00 00 00 00 1A 00 00 00 50 EF 07 1B 93 0A 61 9C 57 30 A3 DB B9 80
20 F1 07 1B 5C F2 07 1B 20 72 F1 76 AC BF CA B0 FE FF FF FF F6 2C F0 76 B0 18
08 40 64 6A 6A 00 00 00 4B 2D C9 D8 69 E5 B9 BE 2F 77 13 0F 89 E6 A3 52 89 8E 96 CC
20 F1 07 1B 00 00 00 00 1C 00 00 00 50 EF 07 1B 2F 77 13 0F 89 E6 A3 52 89 8E 96 CC
20 F1 07 1B 5C F2 07 1B 20 72 F1 76 AC BF CA B0 FE FF FF FF F6 2C F0 76 B0 18 64 6A

How to convert HexString To jpg image in java?

Hi I have a hex String I need to convert that into jpg image please explain me how to do that i have done like this.This Hex String I need to convert into the jpg image,I am trying here but its not coming.
String hex="ff d8 ff e0 00 11 4a 46 49 46 00 01 01 01 00 00 00 00 00 00 0a ff db 00 43 00 08 06 06 07 06 05 08 07 07 07 09 09 08 0a 0c 14 0d 0c 0b 0b 0c 19 12 13 0f 14 1d 1a 1f 1e 1d 1a 1c 1c 20 24 2e 27 20 22 2c 23 1c 1c 28 37 29 2c 30 31 34 34 34 1f 27 39 3d 38 32 3c 2e 33 34 32 ff db 00 43 01 09 09 09 0c 0b 0c 18 0d 0d 18 32 21 1c 21 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 ff c4 00 1f 00 00 01 05 01 01 01 01 01 01 00 00 00 00 00 00 00 00 01 02 03 04 05 06 07 08 09 0a 0b ff c4 00 b5 10 00 02 01 03 03 02 04 03 05 05 04 04 00 00 01 7d 01 02 03 00 04 11 05 12 21 31 41 06 13 51 61 07 22 71 14 32 81 91 a1 08 23 42 b1 c1 15 52 d1 f0 24 33 62 72 82 09 0a 16 17 18 19 1a 25 26 27 28 29 2a 34 35 36 37 38 39 3a 43 44 45 46 47 48 49 4a 53 54 55 56 57 58 59 5a 63 64 65 66 67 68 69 6a 73 74 75 76 77 78 79 7a 83 84 85 86 87 88 89 8a 92 93 94 95 96 97 98 99 9a a2 a3 a4 a5 a6 a7 a8 a9 aa b2 b3 b4 b5 b6 b7 b8 b9 ba c2 c3 c4 c5 c6 c7 c8 c9 ca d2 d3 d4 d5 d6 d7 d8 d9 da e1 e2 e3 e4 e5 e6 e7 e8 e9 ea f1 f2 f3 f4 f5 f6 f7 f8 f9 fa ff c4 00 1f 01 00 03 01 01 01 01 01 01 01 01 01 00 00 00 00 00 00 01 02 03 04 05 06 07 08 09 0a 0b ff c4 00 b5 11 00 02 01 02 04 04 03 04 07 05 04 04 00 01 02 77 00 01 02 03 11 04 05 21 31 06 12 41 51 07 61 71 13 22 32 81 08 14 42 91 a1 b1 c1 09 23 33 52 f0 15 62 72 d1 0a 16 24 34 e1 25 f1 17 18 19 1a 26 27 28 29 2a35 36 37 38 39 3a 43 44 45 46 47 48 49 4a 53 54 55 56 57 58 59 5a 63 64 65";
// String sb = "0d 02 01 02 4e 52 30 39 47 30 35 36 31 38 00 03 00 fa 01 95 e4 53 c0 a8 51 b1 53 a3 03 40 89 54 66 9c ee 10 01 de 9a 5c 22 e6 a0 2c 5d b2 69 8c 9d 79 e6 a5 41 50 c6 78 ab 31 8c 8a 00 72 83 53 2a e0 67 14 d0 2a 54 1b 85 00 00 76 a6 b4 64 1e 95 32 a1 c8 a5 90 f3 81 da 98 15 88 c7 51 54 6e 63 cb 1e 3a d6 99 19 19 ef 55 6e 13 2b 91 49 89 98 2f a7 49 b8 95 20 8a 88 d9 cc b9 f9 0d 6d 85 e6 9e 23 e3 a5 06 7c a6 02 c6 e8 c3 2a 72 0d 74 90 1d d1 29 f5 14 cf 29 5b 82 01 fa 8a 9d 14 00 00 e0 50 38 ad 46 4c 38 15 89 7a b8 9c fa 11 5b f2 2e 56 a9 5c e9 ad 39 0e 18 03 8e f4 8a 68 c4 14 d2 39 ad 07 d2 ae 17 a0 0d f4 35 5d ed 27 4f bd 1b 0f c2 99 9d 88 06 41 ae 9f 4e 19 b2 88 fb 57 36 54 83 c8 23 eb c5 74 da 70 c5 94 7f 4a 07 1d cb 2c 3e 53 58 77 1c ca 45 6e 37 dd 35 89 38 fd e9 a1 97 2d 88 76 d1 8a 76 29 c8 bb 98 52 32 23 da 4f 6a 96 38 01 19 6e 95 21 c0 e0 0a 98 0d d1 0c 75 14 0a c4 b6 d1 a0 8f 81 8c d3 9d 76 1a 92 24 21 00 a8 e7 38 fc 29 9b c7 62 85 c9 dc f8 f4 aa f8 a7 bb 65 89 a6 1e b4 8c a5 ab 0a 43 41 a6 f7 a0 43 5e 95 54 e0 50 06 48 06 a4 c7 14 0e 28 85 86 2a 32 78 a9 5e a0 90 e0 52 35 7b 12 e2 81 4b 8e 69 71 4c 60 bd 6a 74 a8 50 73 53 31 c0 da 3a d0 00 cd b8 e0 74 14 a0 8c e3 bd 20 18 1c 75 a6 a9 cb 8d c3 9c d3 02 c4 7c 1a b8 83 8a aa 8a 59 b8 ab 89 c0 02 80 1e 38 a5 52 41 e2 90 0a 51 40 0f f3 1b 34 ed e1 ba f5 a8 a9 71 40 12 16 e3 02 a2 75 ca 9a 78 a5 c1 34 01 45 46 3a d4 8b 43 ae d9 0f bd 0b 40 89 00 14 f0 05 31 4d 48 a2 81 8e db 9a 70 5a 45 19 a9 00 a6 16 1a 12 83 1e 6a 40 29 c1 68 15 91 58 db 46 df 79 14 fe 15 24 71 88 d7 6a 8c 01 da a6 c5 21 5a 01 22 0d 0a 2a 4b 57 00 0f 00 01 82 03 00 00 0d 0a ";
// String raw_DeviceId = sb.substring(38, 1040);
byte[] b = HexStringToByteArray(hex);
// imageInFile.read(b);
for (int i = 0; i < b.length; i++) {
byte c = b[i];
System.out.println("c = " + c);
}
/*
* Converting Image byte array into Base64 String
*/
String imageDataString = encodeImage(b);
/*
* Converting a Base64 String into Image byte array
*/
byte[] imageByteArray = decodeImage(imageDataString);
/*
* Write a image byte array into file system
*/
FileOutputStream imageOutFile = new FileOutputStream("D:/img6.jpg");
imageOutFile.write(imageByteArray);
imageInFile.close();
imageOutFile.close();
System.out.println("Image Successfully Manipulated!");
} catch (FileNotFoundException e) {
System.out.println("Image not found" + e);
} catch (IOException ioe) {
System.out.println("Exception while reading the Image " + ioe);
}
}
/**
* Encodes the byte array into base64 string
*
* #param imageByteArray - byte array
* #return String a {#link java.lang.String}
*/
public static String encodeImage(byte[] imageByteArray) {
return Base64.encodeBase64URLSafeString(imageByteArray);
}
/**
* Decodes the base64 string into byte array
*
* #param imageDataString - a {#link java.lang.String}
* #return byte array
*/
public static byte[] decodeImage(String imageDataString) {
return Base64.decodeBase64(imageDataString);
}
}
And HexString to bytearray convertion:
public static byte[] HexStringToByteArray(String hexStr) {
int len = hexStr.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(hexStr.charAt(i), 16) << 4)
+ Character.digit(hexStr.charAt(i + 1), 16));
}
return data;
}
Update:
String hex = "ff d8 ff e0 00 11 4a 46 49 46 00 01 01 01 00 00 00 00 00 00 0a ff db 00 43 00 08 06 06 07 06 05 08 07 07 07 09 09 08 0a 0c 14 0d 0c 0b 0b 0c 19 12 13 0f 14 1d 1a 1f 1e 1d 1a 1c 1c 20 24 2e 27 20 22 2c 23 1c 1c 28 37 29 2c 30 31 34 34 34 1f 27 39 3d 38 32 3c 2e 33 34 32 ff db 00 43 01 09 09 09 0c 0b 0c 18 0d 0d 18 32 21 1c 21 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 ff c4 00 1f 00 00 01 05 01 01 01 01 01 01 00 00 00 00 00 00 00 00 01 02 03 04 05 06 07 08 09 0a 0b ff c4 00 b5 10 00 02 01 03 03 02 04 03 05 05 04 04 00 00 01 7d 01 02 03 00 04 11 05 12 21 31 41 06 13 51 61 07 22 71 14 32 81 91 a1 08 23 42 b1 c1 15 52 d1 f0 24 33 62 72 82 09 0a 16 17 18 19 1a 25 26 27 28 29 2a 34 35 36 37 38 39 3a 43 44 45 46 47 48 49 4a 53 54 55 56 57 58 59 5a 63 64 65 66 67 68 69 6a 73 74 75 76 77 78 79 7a 83 84 85 86 87 88 89 8a 92 93 94 95 96 97 98 99 9a a2 a3 a4 a5 a6 a7 a8 a9 aa b2 b3 b4 b5 b6 b7 b8 b9 ba c2 c3 c4 c5 c6 c7 c8 c9 ca d2 d3 d4 d5 d6 d7 d8 d9 da e1 e2 e3 e4 e5 e6 e7 e8 e9 ea f1 f2 f3 f4 f5 f6 f7 f8 f9 fa ff c4 00 1f 01 00 03 01 01 01 01 01 01 01 01 01 00 00 00 00 00 00 01 02 03 04 05 06 07 08 09 0a 0b ff c4 00 b5 11 00 02 01 02 04 04 03 04 07 05 04 04 00 01 02 77 00 01 02 03 11 04 05 21 31 06 12 41 51 07 61 71 13 22 32 81 08 14 42 91 a1 b1 c1 09 23 33 52 f0 15 62 72 d1 0a 16 24 34 e1 25 f1 17 18 19 1a 26 27 28 29 2a35 36 37 38 39 3a 43 44 45 46 47 48 49 4a 53 54 55 56 57 58 59 5a 63 64 65";
// String sb = "0d 02 01 02 4e 52 30 39 47 30 35 36 31 38 00 03 00 fa 01 95 e4 53 c0 a8 51 b1 53 a3 03 40 89 54 66 9c ee 10 01 de 9a 5c 22 e6 a0 2c 5d b2 69 8c 9d 79 e6 a5 41 50 c6 78 ab 31 8c 8a 00 72 83 53 2a e0 67 14 d0 2a 54 1b 85 00 00 76 a6 b4 64 1e 95 32 a1 c8 a5 90 f3 81 da 98 15 88 c7 51 54 6e 63 cb 1e 3a d6 99 19 19 ef 55 6e 13 2b 91 49 89 98 2f a7 49 b8 95 20 8a 88 d9 cc b9 f9 0d 6d 85 e6 9e 23 e3 a5 06 7c a6 02 c6 e8 c3 2a 72 0d 74 90 1d d1 29 f5 14 cf 29 5b 82 01 fa 8a 9d 14 00 00 e0 50 38 ad 46 4c 38 15 89 7a b8 9c fa 11 5b f2 2e 56 a9 5c e9 ad 39 0e 18 03 8e f4 8a 68 c4 14 d2 39 ad 07 d2 ae 17 a0 0d f4 35 5d ed 27 4f bd 1b 0f c2 99 9d 88 06 41 ae 9f 4e 19 b2 88 fb 57 36 54 83 c8 23 eb c5 74 da 70 c5 94 7f 4a 07 1d cb 2c 3e 53 58 77 1c ca 45 6e 37 dd 35 89 38 fd e9 a1 97 2d 88 76 d1 8a 76 29 c8 bb 98 52 32 23 da 4f 6a 96 38 01 19 6e 95 21 c0 e0 0a 98 0d d1 0c 75 14 0a c4 b6 d1 a0 8f 81 8c d3 9d 76 1a 92 24 21 00 a8 e7 38 fc 29 9b c7 62 85 c9 dc f8 f4 aa f8 a7 bb 65 89 a6 1e b4 8c a5 ab 0a 43 41 a6 f7 a0 43 5e 95 54 e0 50 06 48 06 a4 c7 14 0e 28 85 86 2a 32 78 a9 5e a0 90 e0 52 35 7b 12 e2 81 4b 8e 69 71 4c 60 bd 6a 74 a8 50 73 53 31 c0 da 3a d0 00 cd b8 e0 74 14 a0 8c e3 bd 20 18 1c 75 a6 a9 cb 8d c3 9c d3 02 c4 7c 1a b8 83 8a aa 8a 59 b8 ab 89 c0 02 80 1e 38 a5 52 41 e2 90 0a 51 40 0f f3 1b 34 ed e1 ba f5 a8 a9 71 40 12 16 e3 02 a2 75 ca 9a 78 a5 c1 34 01 45 46 3a d4 8b 43 ae d9 0f bd 0b 40 89 00 14 f0 05 31 4d 48 a2 81 8e db 9a 70 5a 45 19 a9 00 a6 16 1a 12 83 1e 6a 40 29 c1 68 15 91 58 db 46 df 79 14 fe 15 24 71 88 d7 6a 8c 01 da a6 c5 21 5a 01 22 0d 0a 2a 4b 57 00 0f 00 01 82 03 00 00 0d 0a ";
// String raw_DeviceId = sb.substring(38, 1040);
// byte[] b = HexStringToByteArray(hex);
//// imageInFile.read(b);
// for (int i = 0; i < b.length; i++) {
// byte c = b[i];
// System.out.println("c = " + c);
//
// }
Font font = new Font("Arial", Font.PLAIN, 12);
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = img.createGraphics();
FontMetrics fm = g2d.getFontMetrics(font);
g2d.dispose();
int width = fm.stringWidth(hex);
int height = fm.getHeight();
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
g2d = img.createGraphics();
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, width, height);
g2d.setColor(Color.BLACK);
g2d.setFont(font);
g2d.drawString(hex, 0, fm.getAscent());
g2d.dispose();
try {
ImageIO.write(img, "jpg", new File("Hex.jpg"));
} catch (IOException ex) {
ex.printStackTrace();
}
You have a kind of chicken and egg issue. In order to generate the image, you will need to know how big to make the image. In order to calculate this information, you will need a Graphics context from an image in order to ascertain the FontMetrics for the given Graphics context...
The following example simple creates a 1x1 BufferedImage which is used to obtain a reference to the FontMetrics, which is used to calculate the required width/height of the String in question.
This is then used to create a new instance of the BufferedImage at the correct size and the String is rendered to it.
It is then saved to a jpg file...
A small snippet of the output image...
String hex="ff d8 ff e0 00 11 4a 46 49 46 00 01 01 01 00 00 00 00 00 00 0a ff db 00 43 00 08 06 06 07 06 05 08 07 07 07 09 09 08 0a 0c 14 0d 0c 0b 0b 0c 19 12 13 0f 14 1d 1a 1f 1e 1d 1a 1c 1c 20 24 2e 27 20 22 2c 23 1c 1c 28 37 29 2c 30 31 34 34 34 1f 27 39 3d 38 32 3c 2e 33 34 32 ff db 00 43 01 09 09 09 0c 0b 0c 18 0d 0d 18 32 21 1c 21 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 ff c4 00 1f 00 00 01 05 01 01 01 01 01 01 00 00 00 00 00 00 00 00 01 02 03 04 05 06 07 08 09 0a 0b ff c4 00 b5 10 00 02 01 03 03 02 04 03 05 05 04 04 00 00 01 7d 01 02 03 00 04 11 05 12 21 31 41 06 13 51 61 07 22 71 14 32 81 91 a1 08 23 42 b1 c1 15 52 d1 f0 24 33 62 72 82 09 0a 16 17 18 19 1a 25 26 27 28 29 2a 34 35 36 37 38 39 3a 43 44 45 46 47 48 49 4a 53 54 55 56 57 58 59 5a 63 64 65 66 67 68 69 6a 73 74 75 76 77 78 79 7a 83 84 85 86 87 88 89 8a 92 93 94 95 96 97 98 99 9a a2 a3 a4 a5 a6 a7 a8 a9 aa b2 b3 b4 b5 b6 b7 b8 b9 ba c2 c3 c4 c5 c6 c7 c8 c9 ca d2 d3 d4 d5 d6 d7 d8 d9 da e1 e2 e3 e4 e5 e6 e7 e8 e9 ea f1 f2 f3 f4 f5 f6 f7 f8 f9 fa ff c4 00 1f 01 00 03 01 01 01 01 01 01 01 01 01 00 00 00 00 00 00 01 02 03 04 05 06 07 08 09 0a 0b ff c4 00 b5 11 00 02 01 02 04 04 03 04 07 05 04 04 00 01 02 77 00 01 02 03 11 04 05 21 31 06 12 41 51 07 61 71 13 22 32 81 08 14 42 91 a1 b1 c1 09 23 33 52 f0 15 62 72 d1 0a 16 24 34 e1 25 f1 17 18 19 1a 26 27 28 29 2a35 36 37 38 39 3a 43 44 45 46 47 48 49 4a 53 54 55 56 57 58 59 5a 63 64 65";
Font font = new Font("Arial", Font.PLAIN, 12);
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = img.createGraphics();
FontMetrics fm = g2d.getFontMetrics(font);
g2d.dispose();
int width = fm.stringWidth(hex);
int height = fm.getHeight();
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
g2d = img.createGraphics();
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, width, height);
g2d.setColor(Color.BLACK);
g2d.setFont(font);
g2d.drawString(hex, 0, fm.getAscent());
g2d.dispose();
try {
ImageIO.write(img, "jpg", new File("Hex.jpg"));
} catch (IOException ex) {
ex.printStackTrace();
}

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

Categories