473,322 Members | 1,401 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,322 software developers and data experts.

Storing and reading different types to/from an array

Jay
I'm trying to store a sequence of operations and values of different types into a single array. It's
a sequence of command word bytes, and a sequence of one or more values (as determined by the command
word) which might be int or double or char (again defined by the command word).

For example, a command byte "MULDBL" would be followed by 2 doubles (each 8 bytes each). This might
be followed by the command byte STRING would be followed by a byte (indicating the length of the
string) and them by some chars.

MULDBL, STRING, and many other command bytes would be part of an enum.

I can do this in C by storing everything in a char array, and using pointers and casts to write the
data into the array, and later read it from the array. How would I do this in C#?
Aug 16 '07 #1
8 2093
Why don't you define an interface like ICommand? And than implement
this
interface for your commands like MulDbl : ICommand and so on.

Aug 16 '07 #2
Jay
Thanks for your reply Roman, but Interfaces are currently too advanced for me since I'm a relative
beginner to C#.

I'm trying to convert a C++ programme to C#, so a solution that doesn't involve too much radical
change would be good.

Jay
"Roman Wagner" <ro**********@gmail.comwrote in message
news:11********************@w3g2000hsg.googlegroup s.com...
Why don't you define an interface like ICommand? And than implement
this
interface for your commands like MulDbl : ICommand and so on.
Aug 16 '07 #3
On 16 Aug, 09:55, "Jay" <nospamwrote:
Thanks for your reply Roman, but Interfaces are currently too advanced for me since I'm a relative
beginner to C#.

I'm trying to convert a C++ programme to C#, so a solution that doesn't involve too much radical
change would be good.

Jay

"Roman Wagner" <roman.wag...@gmail.comwrote in message

news:11********************@w3g2000hsg.googlegroup s.com...
Why don't you define an interface like ICommand? And than implement
this
interface for your commands like MulDbl : ICommand and so on.
There are loads of ways you can convert types in C#. Here are some
examples:

double d = 1.123456;
string s;
int i = 9;
string to double:
s = "1.123456";
d = double.Parse(s); //in framework 2 you can also look at TryParse.

double to string
s = d.ToString("0.00"); //"0.00" is a format string, there are loads
of options for this.

double to int
d = double.MaxValue;
i = (int)d; // This cast will work, but double.MaxValue is much bigger
than an int can hold so it will truncate.

As you can see the types have both static and non static methods and
you can use casts.

Any use?

Aug 16 '07 #4
Hello Jay,

Look at MemoryStreams with binarywriters/textwriters

MemoryStream stream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(stream);
writer.Write((int)3);
stream.getbuffer()
"Jay" <nospamwrote in message
news:eY**************@TK2MSFTNGP05.phx.gbl...
I'm trying to store a sequence of operations and values of different types
into a single array. It's
a sequence of command word bytes, and a sequence of one or more values (as
determined by the command
word) which might be int or double or char (again defined by the command
word).

For example, a command byte "MULDBL" would be followed by 2 doubles (each
8 bytes each). This might
be followed by the command byte STRING would be followed by a byte
(indicating the length of the
string) and them by some chars.

MULDBL, STRING, and many other command bytes would be part of an enum.

I can do this in C by storing everything in a char array, and using
pointers and casts to write the
data into the array, and later read it from the array. How would I do this
in C#?


Aug 16 '07 #5
On Aug 16, 9:55 am, "Jay" <nospamwrote:
Thanks for your reply Roman, but Interfaces are currently too advanced
for me since I'm a relative beginner to C#.

I'm trying to convert a C++ programme to C#, so a solution
that doesn't involve too much radical change would be good.
I would *strongly* recommend that you take the interface approach.
Interfaces aren't that hard to learn, and you'll need to know about
them anyway.

It's a short term hit to make the code much cleaner in the long term.

Jon

Aug 16 '07 #6
Jay
Thanks for your replies everyone.

John: "I would *strongly* recommend that you take the interface approach." OK, I will have another
look at the chapter on Interfaces in my C# book tonight.

Claire: "Look at MemoryStreams with binarywriters/textwriters". If Interfaces still elude me, I will
look into this.

Jay
"Jay" <nospamwrote in message news:eY**************@TK2MSFTNGP05.phx.gbl...
I'm trying to store a sequence of operations and values of different types into a single array. It's
a sequence of command word bytes, and a sequence of one or more values (as determined by the command
word) which might be int or double or char (again defined by the command word).

For example, a command byte "MULDBL" would be followed by 2 doubles (each 8 bytes each). This might
be followed by the command byte STRING would be followed by a byte (indicating the length of the
string) and them by some chars.

MULDBL, STRING, and many other command bytes would be part of an enum.

I can do this in C by storing everything in a char array, and using pointers and casts to write the
data into the array, and later read it from the array. How would I do this in C#?

Aug 16 '07 #7
Declare an Arraylist, each location takes an object, which can be an enum, a
double, a string of characters, or anything else you can think of. Since you
will always know what you are taking out of the arraylist, you can cast the
object you get back to the right type. You will find the whole process
actually far simpler than the C equivalent.

"Jay" wrote:
I'm trying to store a sequence of operations and values of different types into a single array. It's
a sequence of command word bytes, and a sequence of one or more values (as determined by the command
word) which might be int or double or char (again defined by the command word).

For example, a command byte "MULDBL" would be followed by 2 doubles (each 8 bytes each). This might
be followed by the command byte STRING would be followed by a byte (indicating the length of the
string) and them by some chars.

MULDBL, STRING, and many other command bytes would be part of an enum.

I can do this in C by storing everything in a char array, and using pointers and casts to write the
data into the array, and later read it from the array. How would I do this in C#?
Aug 17 '07 #8
Jay
I'm afraid that after reading my C# book, Interfaces still haven't clicked, and timescales dictate
that I must use one of the other two suggestions: ArrayList or MemoryStream.

Am I right in saying that MemoryStream is faster (since ArrayList uses objects)?

Jay

"Jay" <nospamwrote in message news:eY**************@TK2MSFTNGP05.phx.gbl...
I'm trying to store a sequence of operations and values of different types into a single array. It's
a sequence of command word bytes, and a sequence of one or more values (as determined by the command
word) which might be int or double or char (again defined by the command word).

For example, a command byte "MULDBL" would be followed by 2 doubles (each 8 bytes each). This might
be followed by the command byte STRING would be followed by a byte (indicating the length of the
string) and them by some chars.

MULDBL, STRING, and many other command bytes would be part of an enum.

I can do this in C by storing everything in a char array, and using pointers and casts to write the
data into the array, and later read it from the array. How would I do this in C#?

Aug 17 '07 #9

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

Similar topics

3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
9
by: Arnold | last post by:
I need to read a binary file and store it into a buffer in memory (system has large amount of RAM, 2GB+) then pass it to a function. The function accepts input as 32 bit unsigned longs (DWORD). I...
5
by: matevz bradac | last post by:
Hi, I'm trying to implement delayed function execution, similar to OpenGL's display lists. It looks something like this: beginList (); fcn1 (); fcn2 ();
22
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a...
13
by: Rhino | last post by:
Is it possible to store Java objects in DB2 V8.2 for Windows/Unix/Linux via JDBC? Specifically, if I have a 4-dimensional boolean array, i.e. boolean, can I store it directly in a column of a...
20
by: Martin Jørgensen | last post by:
Hi, I'm reading a number of double values from a file. It's a 2D-array: 1 2 3 4 5 6 7 ------------- 1 3.2 2 0 2.1 3 9.3 4
2
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
10
by: Tyler | last post by:
Hello All: After trying to find an open source alternative to Matlab (or IDL), I am currently getting acquainted with Python and, in particular SciPy, NumPy, and Matplotlib. While I await the...
10
by: deciacco | last post by:
I'm writing a command line utility to move some files. I'm dealing with thousands of files and I was wondering if anyone had any suggestions. This is what I have currently: $arrayVirtualFile =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.