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

  • Thread starter Thread starter SteveK
  • Start date Start date
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
 
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.
 
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
 
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
 
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
 
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.
 
Thanks Jon, that is a good thing to have pointed out ;)
I will look into this stuff more...

-SK
 
Back
Top