convert byte[] to an type

  • Thread starter Thread starter sklett
  • Start date Start date
S

sklett

I wasn't sure how to phrase the subject.

Currently I'm storing a file as a byte[] in a database, when I want to work
with the file I download the byte[] and write the data to a file on disk
then loading the file from disk and working with it in memory. Pretty
standard.

I'm now thinking that it would be MUCH better if I could somehow convert the
byte[] to the object in memory. If I was dealing with binary data I could
use BinaryFormatter (I think), but since I'm using byte[] I'm not sure hwo
to do this? Is it even possible?

Here is some pseudo code:
byte[] data = GetFileData();

FileObject fileObject = SomeMethodThatWillConvertByteDataToAnObject();

// Work with the 'deserialized' FileObject instance.
<end code sample>

Is this possible? I don't even really know what to search for?

Thanks,
Steve
 
byte[] *is* binary data; what type of object are you looking to get
back at the end? For a raw file, then bye[] is a reasonable
representation; for serialized objects you would use the appropriate
serializer, loading the byte[] into a MemoryStream first; for simple
objects (numbers, dates, etc) BitConverter is the norm.

So: what is the object that you are trying to rehydrate? hint: you
have to know this first.

Marc
 
Hi Marc,

Thanks for the info. The object I'm trying to create is an object from an
label program API. I've serialized a file from disk to a byte array. The
api want me to create one of these label objects by passing it a path to a
file... this is slowing things down quite a bit.

What I wanted to do was just convert the byte array to one of the label
objects. I don't know anything about the label object, it's my hope that
it's a simple representation of the file on disk.

I hope that makes sense. When I've done this in the past with simple
structures (ie: fixed size structures) it's been very simple. If they are
using CStrings and other dynamically sized types then I will be SOL. It's
worth a shot though, I've asked support and they won't answer me ;)

I do realize there is a slim chance it will work, but I figure it's worth a
shot.

-Steve
 
(for larger objects you might want to grad the stream directly from
the DBMS; the "how" here changes per vendor, so google for how to get
a BLOB as a Stream for your vendor - or post more info here)

Marc
 
In that scenario, I'd suggest sticking with the API and using temp
files, especially if the API isn't .Net

You could try parsing the contents, but...

Marc
 
sklett said:
Hi Marc,

Thanks for the info. The object I'm trying to create is an object from an
label program API. I've serialized a file from disk to a byte array. The
api want me to create one of these label objects by passing it a path to a
file... this is slowing things down quite a bit.

Then you have to have a path to a file. However, in *nix, and to a slightly
lesser extent in Windows, everything is a file, not just disk files. You
may be able to create a named pipe and hand the path to said pipe to the
API, thereby avoiding slow disk access.
 
Back
Top