How do I Get Around Using Copymemory???

A

Alex

I have been using VS.net now for a while mainly using ASP.Net however I feel
ready to start upgrading applications to VS.Net.

However most of my previous VB applications used Copymemory regularly to do
the following things

1. Join and split variabes such as long to 2 integers and back again
2. converting Binary data in to User defined type stuctures and back again
3. pack various variables, strings etc. into byte arrays which i could read
and write to and from files.

I like managed code and would like to avoid unsafe code in c#.

I regularly save structures to and from binary files and I want to be able
to convert these things. I have found a number of workarounds for most
things however this is an area I need to conquer before can convert the
applications.

Another thing I would do was copy a stream of variables into a byte array
and then write it to file and then later read it from the file into a
bytearray and pull out the variables using a pointer to each part of the
byte array.

also Does anyone know any way to split variables and combine them e.g. 2
bytes into a short.
I know that this can be done with masks however I am struggling to do this
efficiently since my maths in this are is a bit lacking and I am getting
overflows when working with signed variables.

Any help with the above would be great

Thanks Alex
 
C

Cablewizard

Alex,

I primarily use VB.Net and not C#, but the answer is basically the same...
For all your Byte level manipulations, explore the System.BitConverter class.
While it may not handle full UDT's (Structures) as easily as CopyMemory, it will
give you pretty much everything you need.
For Text manipulations, explore the System.Text.Encoding class.
Subclasses allow you do do most things text related as in streaming to bytes and
back.
Additionally, you can choose which encoding to use; ASCII, UTF, Unicode, etc.

For example, say you have a Byte array (myBytes) with 2 bytes to represent a
Short (myShort) at index 2 in the array.
myShort = System.BitConverter.ToInt16(myBytes, 2)

Hope this helps,
Gerald
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top