Paul Clapham wrote:Sure, both of those things exist. And they are right there in the documentation for java.lang.String. You can't miss them.
Kevin Simonson wrote:So, taking the deprecated code and the fact that both of the other algorithms potentially lose data, into consideration, it looks like my way of converting <String>s into <byte> arrays is probably the best of the three ways.
What do you guys think?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:Lots. I hope you don't mind.
Winston Gutkowski wrote:1. Your method only does a default String-->byte[] conversion; String's methods do all sorts.
2. It seems like re-inventing the wheel for an extraordinarily small difference in time (I did read it right - 1.1 seconds overall for 364,000 lines).
3. I hope you've tested your code exhaustively so that you know what happens in all corner cases (I'm pretty sure Sun did).
Winston Gutkowski wrote:4. What could be easier that s.getBytes() or String s = new String(bytes)?
Winston Gutkowski wrote:5. I refer you to my quote.
Kevin Simonson wrote:
Ease is nice, but correctness is pretty important too, and is probably more important than ease. I'm looking for a way to convert <String>s to <byte> arrays in such a way that I can convert them back to <String>s and get the same values.
Kevin Simonson wrote:I couldn't find your quote. What quote were you referring to?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Kevin Simonson wrote:
Paul Clapham wrote:Sure, both of those things exist. And they are right there in the documentation for java.lang.String. You can't miss them.
As I stated in my original post, I was primarily interested in a quick way to transform a <String> argument into an array of <byte>s. I followed the link you gave me, Paul, and tried timing it on transforming <String> objects into <char> arrays, and then back again, both with the <getBytes( int, int, byte[], int)> method one way and the <String( byte[], int, int}> constructor the other way, and with the <getBytes()> method (that returns an object of type <byte[]>) the first way, and with the <String( byte[])> constructor the other way....
What do you guys think?
Kevin Simonson wrote:That's part of the problem; I tested both my code and the code that uses Sun's constructors. My test code included:
When I compile and run it I get:
C:\Users\kvnsmnsn\K\Java\Misc>javac TsCheck.java
Note: TsCheck.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
C:\Users\kvnsmnsn\K\Java\Misc>java TsCheck
problem.equals( result_l9) == false.
problem.equals( result_vl) == false.
buffer:
0: 97
1: 97
2: 97
3: 97
4: -1
5: -1
6: -1
7: -1
8: -1
vlBuffer:
0: 97
1: 63
2: 63
3: 63
Kevin Simonson wrote:
Did anybody notice those two lines up there right under the "java TsCheck" command? The result of calling <problem.equals( result_l9)> was <false>! The result of calling <problem.equals( result_vl)> was also <false>! In other words, using the built-in <getBytes()> methods and the built-in constructors, I got back <String>s that contained different <char>s than when I started.
So what good is increased speed of execution or ease of calling methods and constructors when the quickly executing and easily called methods and constructors don't actually do the job?
Kevin Simonson wrote:The actual <String>s I'm going to be using this for are file names and directory names. Now ordinarily my tool is going to be used on machines where file names and directory names are made up of ASCII <char>s, so the built-in methods and constructors would work just fine. But I'd like my tool to be general purpose so that it can work on anybody's file names and directory names. Is it impossible to name a file or directory using one or two <char>s whose actual values get up around <(char) 21857> or <(char) 43617> or <(char) 65377> like I have in my program?
Nothing up my sleeve ... and ... presto! A tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|