473,395 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
Nov 16 '05 #1
7 1235
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]
- mv*@spam.guard.caspershouse.com

"SteveK" <as***@asdfasdfsd.com> wrote in message
news:OW**************@TK2MSFTNGP14.phx.gbl...
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

Nov 16 '05 #2
Thanks Nicholas,

I have BitConverter working nicely, I will look into the Encoding solution
you suggested.
Thanks again!
Steve
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP10.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"SteveK" <as***@asdfasdfsd.com> wrote in message
news:OW**************@TK2MSFTNGP14.phx.gbl...
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


Nov 16 '05 #3
Why is class Encoding abstract? Seems like the base class has what I
need... confused.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP10.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"SteveK" <as***@asdfasdfsd.com> wrote in message
news:OW**************@TK2MSFTNGP14.phx.gbl...
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


Nov 16 '05 #4
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" <as***@asdfasdfsd.com> napisał w wiadomo¶ci
news:uc**************@TK2MSFTNGP09.phx.gbl...
Why is class Encoding abstract? Seems like the base class has what I
need... confused.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:%2****************@TK2MSFTNGP10.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"SteveK" <as***@asdfasdfsd.com> wrote in message
news:OW**************@TK2MSFTNGP14.phx.gbl...
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



Nov 16 '05 #5
lukasz <bb****@op.pl> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
SteveK <as***@asdfasdfsd.com> wrote:
Why is class Encoding abstract? Seems like the base class has what I
need... confused.


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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
Thanks Jon, that is a good thing to have pointed out ;)
I will look into this stuff more...

-SK
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
SteveK <as***@asdfasdfsd.com> wrote:
Why is class Encoding abstract? Seems like the base class has what I
need... confused.


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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
8
by: Ben Terry | last post by:
What's the most efficient way to transfer data from a byte to a struct? The struct is rather complex--contains other structs as well as byte members. I've tried to use Marshal.Copy and an IntPtr...
3
by: TJO | last post by:
Can someone recomend a good technique for removing BOF and EOF markers from a byte? I am passing a byte thru an FTP socket. I am serializing an object into a byte and then adding an EOF marker...
5
by: Gianmaria I. | last post by:
Hi, having a BitArray, how can i extract bits to create a System.byte as in the example... With BitArray bits and Byte myNewByte
2
by: twawsico | last post by:
I have a piece of code that needs to read the contents of a binary file (that I've created with another app) into an array of structures. The binary data in the file represents just a series of...
3
by: Terry | last post by:
In the code below, I open a file using filestream, copy it to a byte array, and the write that array out to a new file. But when I check the size of the original file and the new file, the new...
3
by: marfi95 | last post by:
Hi all. I need to copy a byte array into a string, but starting at a specific location in the byte array. This is where I get hung up. For example if my byte array is (100) big, I might want to...
12
by: O.B. | last post by:
I'm trying to do a static_cast at runtime in C# and as I understand it, "as" is the keyword to use. Unfortunately, the compiler is trying to do the cast a compilation time. See below. /*...
4
by: Rainer Queck | last post by:
Hi NG, is there a way to copy a buffer pointed to by a IntPtr directly into a two dimensional byte-array? I tried this, what obviously doesn't work: byte image = new byte; IntPtr p =...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.