Results 1 to 4 of 4
Thread: Compare different size arrays
- 05-03-2012, 12:22 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Compare different size arrays
Hello,
I have this issue. I want to compare different size arrays to check my data i got from a serial line. I've got it working but i doesn't feel like the right way to do it. Here is my code :
So basicly, i call the method CheckData with a buffer of 1024 bytes (can't be changed). Now i want to compare the first 4 bytes of this buffer with a 4-byte KeepAlive sequence. If equal, then send back ACK.Java Code:public void CheckData(byte[] buffer, int length) { byte [] KeepAlive = new byte[1024]; KeepAlive[0] = 0x31; KeepAlive[1] = 0x05; KeepAlive[2] = 0x32; KeepAlive[3] = 0x05; byte [] ACK = new byte [1024]; ACK[0] = 0x06; if (Arrays.equals(buffer, KeepAlive)) { try { outputStream.write(ACK); } catch (IOException ex) {} } }
The above example does work, but a more professional approach would be welcome :)
Thanks in advance!
- 05-03-2012, 01:31 PM #2
Re: Compare different size arrays
What do you want the results of the compare to be if the arrays are different sizes? What does the Arrays equals() method do when the arrays are different sizes?
You could use a for loop and compare each element of the two arrays.If you don't understand my response, don't ignore it, ask a question.
- 05-03-2012, 02:20 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Compare different size arrays
I'm amazed the above works, because the keepAlive array has 1020 entries that are all 0 so, unless the given buffer also only has 0 in the end 1020 bytes it will never give true.
If you want to compare just the first 4 bytes then keepAlive should be byte[4] and the call would be equals(Arrays.copyOf(buffer, 4), keepAlive).
Also I would suggest turning keepAlive into a static final class constant, KEEP_ALIVE.Please do not ask for code as refusal often offends.
- 05-03-2012, 02:35 PM #4
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Re: Compare different size arrays
@ Tolls
Indeed, when the keep alive package is send. The complete buffer has all 0 in the last 1020 bytes. If not, something is wrong. But as I said, this code works.
Now, I altered my code and used the copyOf method and still got succes. This feels like nicer way to do it. Thanks!
Also thanks for the tip for using final class constants.
Similar Threads
-
Compare two Arrays, checking for duplication
By Redefine12 in forum New To JavaReplies: 4Last Post: 02-03-2012, 09:57 PM -
compare arrays
By innspiron in forum New To JavaReplies: 4Last Post: 04-16-2010, 10:22 PM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
Compare two arrays for difference
By aaronfsimons in forum New To JavaReplies: 2Last Post: 05-11-2009, 03:49 PM -
Setting frame size to the size of an image
By Yoruichi in forum AWT / SwingReplies: 5Last Post: 04-22-2009, 04:37 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks