473,830 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2126
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**********@g mail.comwrote in message
news:11******** ************@w3 g2000hsg.google groups.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...@g mail.comwrote in message

news:11******** ************@w3 g2000hsg.google groups.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.0 0"); //"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(st ream);
writer.Write((i nt)3);
stream.getbuffe r()
"Jay" <nospamwrote in message
news:eY******** ******@TK2MSFTN GP05.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******** ******@TK2MSFTN GP05.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******** ******@TK2MSFTN GP05.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
11773
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 table in MySQL with the path & filename to the image. I have successfully uploaded and performed an update query on the database, but the problem I have is I cannot retain the primary key field in a variable which is then used in a SQL update...
9
4074
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 can pass a max of 512 words to it at a time. So I would pass them in chunks of 512 words until the whole file has been processed. I haven't worked with binary files before so I'm confused with how to store the binary file into memory. What sort of...
5
411
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
2468
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 nightmare. There are of course many solutions, but they all end up forcing you to abandon the array syntax in favour of macros or functions. Now I have two questions - one is historical, and the other practical. 1.) Surely malloc (and...
13
3716
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 DB2 table? If so, how do I do it? It would be VERY convenient if I could store this boolean array directly in a column of a DB2 table but I'm not at all clear on whether this is possible, even after reading the documentation in the Information...
20
4660
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
7057
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, input box for units of service, description of the service and each row has its own dropdown list of unit fees that apply. Each dynamically created row will return 3 values fee1_choice, fee1_unit and fee1_money. Note The above informaton is...
10
8365
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 delivery of Travis Oliphant's NumPy manual, I have a quick question (hopefully) regarding how to read in Fortran written data. The data files are not binary, but ASCII text files with no formatting and mixed data types (strings, integers,...
10
2315
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 = array( 'filename'=>'filename', 'basename'=>'filename.ext', 'extension'=>'ext', 'size'=>0,
0
9642
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10777
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10493
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10206
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7747
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5617
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5780
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4416
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3076
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.