Results 1 to 15 of 15
Thread: Expressing a byte string
- 11-25-2010, 04:29 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 5
- Rep Power
- 0
Expressing a byte string
I need to know how to express a byte string literal in Java. I also need to compare it to a string of bytes that I'm reading from a file. I know how I'd do this in C. I'd have something like "\x000\x001\x002" (I think) then I'd then compare it with memcmp(). How do I do the same in Java?
- 11-25-2010, 04:38 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
From where you find a byte string literal in Java?
- 11-25-2010, 04:47 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 5
- Rep Power
- 0
If I could find one, I'm sure I could figure out how to make one.
- 11-25-2010, 04:52 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
That's what I want to flag.
In C/C++ you can convert byte array to string in a sense that human can understand. If the byte array contain some annoying characters, by converting each you can build a meaningful string.
However Java doesn't have a direct way to do that.
- 11-25-2010, 05:15 AM #5
Member
- Join Date
- Nov 2010
- Posts
- 5
- Rep Power
- 0
So, what you're saying is if I wanted to find out if the first byte in a byte array was 8, and the next was 3, and the next was 2, while I could do
in C, there isn't a one-liner in Java as simple that can accomplish this? Is the best I can do:Java Code:if (memcmp("\x08\x03\x02", pByteArray, 3) == 0)
?Java Code:if (byteArray[0] == 8 && byteArray[1] == 3 && byteArray[2] == 2)
- 11-25-2010, 05:27 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
As far as I know there is no any other way to do that.
-
C is a much more terse language than Java. It's also much easier to shoot yourself in the foot with C/C++.
- 11-25-2010, 05:59 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 11-25-2010, 06:07 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
If you often have to check whether one array starts with elements equal to some other array you would write a method for that.
The array you are comparing with might be expressed, reasonabley tersely, as
Java Code:new byte[] {8, 3, 2}
- 11-25-2010, 06:14 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
So that method should have a way to convert values into a common format.
- 11-25-2010, 08:14 AM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
I'm not sure what you mean. I had in mind something like:
Java Code:boolean compare(byte[] arr, byte[] with) { if(arr.length < with.length) { return false; } for(int i = 0; i < with.length; i++) { if(arr[i] != with[i]) { return false; } } return true; }
Usage would be like:
Java Code://if (memcmp("\x08\x03\x02", pByteArray, 3) == 0) if(compare(byteArray, new byte[] {8, 3, 2}))
- 11-25-2010, 08:51 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
That's in same type. I thought in the other array there are string literals, with the number in another array (byte array actually)
- 11-25-2010, 07:43 PM #13
Member
- Join Date
- Nov 2010
- Posts
- 5
- Rep Power
- 0
Ok, now I've discovered another problem I'm going to have w/Java: no unsigned types. Quite literally, the line I have is:
and my parser has flagged all those bytes >0x7f as errors. The recommendation is that they be cast to bytes. You now know that what I want to do is compare a byte array with another byte array, and I'm just wondering what's going to happen w/the bytes that are cast. Will they become negatives, and the ones that are 0x88 will remain 0x88, (and then somehow be internally imaged as -120) or will something else happen to them?Java Code:private static byte[] _authenticationBytes = new byte[] { 0x01, 0x67, 0x22, 0x32, 0xa7, 0x0f, 0xef, 0x32, 0x65, 0x77, 0x62, 0x99, 0x88, 0x87, 0x11, 0x17 };
- 11-25-2010, 08:23 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-25-2010, 10:47 PM #15
Member
- Join Date
- Nov 2010
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Converting string to byte[]
By bobo67 in forum New To JavaReplies: 12Last Post: 09-10-2010, 09:10 PM -
String byte array
By myka in forum Advanced JavaReplies: 2Last Post: 03-18-2010, 02:33 AM -
String from byte array
By justint in forum New To JavaReplies: 2Last Post: 01-22-2010, 06:58 AM -
Search a string in a byte array
By 2BOrNot2B in forum New To JavaReplies: 0Last Post: 03-12-2009, 05:52 PM -
String byte storage
By bozovilla in forum New To JavaReplies: 1Last Post: 11-24-2007, 06:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks