so I have a byte[], how to copy to int, string, etc..

S

SteveK

I'm trying to write a quick migrate tool to convert our old binary flat DB
to SQL Server. I thought I would use C# and get started with a managed
language. I have found plenty of examples of how to read a binary file into
an array of bytes, great. But what do I do with it then?

How can I copy n bytes from my data byte[] into the new, managed float
object that I just declared? What am I missing? Any ideas?

Thanks for any help, this is one of those problems that once solved will
open the flood gates of success ;)

-Steve
 
N

Nicholas Paldino [.NET/C# MVP]

Steve,

You can use the static methods on the BitConverter class to convert to
value types (assuming the byte layout is the same).

For strings, you will want to use a class derived from
System.Text.Encoding, and then pass the bytes to the GetString method (based
on what encoding the strings are stored in).

Hope this helps.
 
S

SteveK

Thanks Nicholas,

I have BitConverter working nicely, I will look into the Encoding solution
you suggested.


Thanks again!
Steve


Nicholas Paldino said:
Steve,

You can use the static methods on the BitConverter class to convert to
value types (assuming the byte layout is the same).

For strings, you will want to use a class derived from
System.Text.Encoding, and then pass the bytes to the GetString method (based
on what encoding the strings are stored in).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

SteveK said:
I'm trying to write a quick migrate tool to convert our old binary flat DB
to SQL Server. I thought I would use C# and get started with a managed
language. I have found plenty of examples of how to read a binary file
into
an array of bytes, great. But what do I do with it then?

How can I copy n bytes from my data byte[] into the new, managed float
object that I just declared? What am I missing? Any ideas?

Thanks for any help, this is one of those problems that once solved will
open the flood gates of success ;)

-Steve
 
S

SteveK

Why is class Encoding abstract? Seems like the base class has what I
need... confused.


Nicholas Paldino said:
Steve,

You can use the static methods on the BitConverter class to convert to
value types (assuming the byte layout is the same).

For strings, you will want to use a class derived from
System.Text.Encoding, and then pass the bytes to the GetString method (based
on what encoding the strings are stored in).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

SteveK said:
I'm trying to write a quick migrate tool to convert our old binary flat DB
to SQL Server. I thought I would use C# and get started with a managed
language. I have found plenty of examples of how to read a binary file
into
an array of bytes, great. But what do I do with it then?

How can I copy n bytes from my data byte[] into the new, managed float
object that I just declared? What am I missing? Any ideas?

Thanks for any help, this is one of those problems that once solved will
open the flood gates of success ;)

-Steve
 
L

lukasz

You create instances of classes derived from Encoding which represent your
encoding:
new UnicodeEncoding().GetString(arr)
new UTF8Encoding().GetString(arr)
and so on

U¿ytkownik "SteveK said:
Why is class Encoding abstract? Seems like the base class has what I
need... confused.


message news:%[email protected]...
Steve,

You can use the static methods on the BitConverter class to convert to
value types (assuming the byte layout is the same).

For strings, you will want to use a class derived from
System.Text.Encoding, and then pass the bytes to the GetString method (based
on what encoding the strings are stored in).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

SteveK said:
I'm trying to write a quick migrate tool to convert our old binary
flat
DB
to SQL Server. I thought I would use C# and get started with a managed
language. I have found plenty of examples of how to read a binary file
into
an array of bytes, great. But what do I do with it then?

How can I copy n bytes from my data byte[] into the new, managed float
object that I just declared? What am I missing? Any ideas?

Thanks for any help, this is one of those problems that once solved will
open the flood gates of success ;)

-Steve
 
J

Jon Skeet [C# MVP]

lukasz said:
You create instances of classes derived from Encoding which represent your
encoding:
new UnicodeEncoding().GetString(arr)
new UTF8Encoding().GetString(arr)
and so on

Except that usually using the properties on Encoding is nicer:

Encoding.Unicode.GetString(arr);
Encoding.UTF8.GetString(arr);

I only create a new Encoding instance if I need it to have specific
parameters which the version retrieved by the property may not.
 
J

Jon Skeet [C# MVP]

S

SteveK

Thanks Jon, that is a good thing to have pointed out ;)
I will look into this stuff more...

-SK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top