so I have a byte, how to copy to int, string, etc..
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.csharp.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

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

Reply to this message...
 
    
Nicholas Paldino [.NET/C# MVP] (VIP)
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]
- Click here to reveal e-mail address

"SteveK" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
SteveK
Thanks Nicholas,

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

Thanks again!
Steve

"Nicholas Paldino [.NET/C# MVP]" <Click here to reveal e-mail address> wrote in
message news:%Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
SteveK
Why is class Encoding abstract? Seems like the base class has what I
need... confused.

"Nicholas Paldino [.NET/C# MVP]" <Click here to reveal e-mail address> wrote in
message news:%Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
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" <Click here to reveal e-mail address> napisa³ w wiadomo¶ci
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Jon Skeet [C# MVP] (VIP)
lukasz <Click here to reveal e-mail address> wrote:
[Original message clipped]

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.

--
Jon Skeet - <Click here to reveal e-mail address>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply to this message...
 
    
Jon Skeet [C# MVP] (VIP)
SteveK <Click here to reveal e-mail address> wrote:
[Original message clipped]

See http://www.pobox.com/~skeet/csharp/unicode.html

You might also want to consider using BinaryReader instead of
BitConverter. It may make some things easier for you.

Finally, BitConverter has a problem - it only works with one
endianness, and that can't be set :( I have a library which solves this
problem:
http://www.pobox.com/~skeet/csharp/miscutil" target="_blank">http://www.pobox.com/~skeet/csharp/miscutil

--
Jon Skeet - <Click here to reveal e-mail address>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply to this message...
 
 
System.BitConverter
System.IO.BinaryReader
System.Text.Encoding
System.Text.UnicodeEncoding
System.Text.UTF8Encoding




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com