• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SCDE: how to convert bytes[] to long?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 493
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bharat,
I am so happy to see you share my point of view.
Keep in touch.
--ds
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic