Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SCDE: how to convert bytes[] to long?

 
Donald Scott
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi every body,
I am getting crazy: how do you convert byte[] to long? Am asking this question, because in my SCDE assignment, I am given a database file (organized according a byte[] format) and I have to implement a data acess class "Data.java" which looks like as folows:
package suncertify.db;
public interface DBAccess
{
...
//Reads a record from the file. Returns an array where each element is a record value.
public Strng[] readRecord(long recNo) throws RecordNotFounndException
...
}
I get lost. Need help.
-- ds
 
Paul Tongyoo
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's probably a way better way to do this, but I believe the following code will work:


But it looks like the data file you're getting is the same as what we're all getting. Have you looked into the java.nio package and using ByteBuffers? Max Habibi's book has a great chapter on the useful parts of this package.
Regards,
Paul
[ November 06, 2003: Message edited by: Paul Tongyoo ]
 
Richard Jackson
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,Paul and others
I also face the same problem.In my assignment,the type of recNo is alsolong.
Inspite that you said,what other better ways to solve the problem?
Regards,
Richard
 
Paul Tongyoo
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh! I'm terribly sorry-- I didn't read close enough to catch that recNo of type long. Can you be more specific to what "the problem" is? If it's interpretting a "long recNo", then you're okay if you intend to use FileChannels to access your data file. The FileChannel.read(..) function takes a value of type "long" as a parameter. Since you'll probably be using the recNo parameter as a way to calculate the offset of the record in your file, you should be okay here.
(I am unfamiliar with the "byte[] format" of the data file that Donald mentioned. Maybe you could be a little bit more descriptive?)

Regards,
Paul
 
Donald Scott
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi colleagues:
I think I understand my problem now. The "byte[] format" I was referring to means that the database file is organized as follows:
each record (in bytes) has a fixed length. So, it sounds to me that we should treat a record as an array of bytes. (I tested this without any problem. I was able to read the whole file.)
But my misunderstanding came from the way the "DBAccess" interface was described. The author (SUN) gives the signature of the readrecord method as follows:
...
public String [] readRecord(long recNo) throws RecordNotFoundException;
...
This method intends to read a record from the file...
So, I understood that "recNo" refers here to the data contained in a record (but in a * long * fornat). I discovered, after so much time, my error: the *recNo here stands for the record position in the database file. Correct me if I am wrong, but I think I am right.
Regards,
--ds
 
Bharat Ruparel
Ranch Hand
Posts: 493
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Don,
You wrote:

each record (in bytes) has a fixed length. So, it sounds to me that we should treat a record as an array of bytes. (I tested this without any problem. I was able to read the whole file.)

Quite correct! If you are using the RandomAccessFile, look-up the readFully method.

I discovered, after so much time, my error: the *recNo here stands for the record position in the database file. Correct me if I am wrong, but I think I am right.

Correct again!
Good job!
And welcome to the Ranch.
Regards.
Bharat
 
Donald Scott
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bharat,
I am so happy to see you share my point of view.
Keep in touch.
--ds
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic