473,322 Members | 1,501 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.

Efficiently convert Base 1 Array to Base 0 Array?

Hi all,

My scenario is as follows:

* Receive a base 1 array (index starts at 1 instead of 0) of data from
a COM component .
* Need to pass this array to a .net component that requires its array
index to start at 0.
* As a result, must convert array from base1 to base 0.

Currently my solution for this is to create a new array and copy the
contents of is as follows:
* Create new based 0 array
* Copy the content of base 1 array to new array by 1)iterating through
each element in base 1 array and 2)doing assignment to base 0 array.
* Assign new array to array used by .net component described above.

My question is as follows:
Is there a quicker way to convert a base1 array to base0 without
having to do iteration and assignment? Does something in the .net
framework already do this?

Thanks,
Peter
www.nofelt.com

Feb 8 '06 #1
9 7144
Hi,

"AtariPete" <pc******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi all,

My scenario is as follows:

* Receive a base 1 array (index starts at 1 instead of 0) of data from
a COM component .
* Need to pass this array to a .net component that requires its array
index to start at 0.


Just for your clarification, the "start" of the array is something
artificial based on the language used, in reality what you have is a
continuos chunk of memory divided in N equals parts where each part hold an
element of the array.
The index is used to indicate the compiler the offset from the start to get
the element you want. therefore is "artificial" , what I mean is that to
access the first element in that very same array you use index 0 or 1
depending of the language you use.

IIRC in some old version of pascal (or was basic ) you could even define
the start index of the array. it could be negative !
you do not need to create a new array and copy it.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Feb 8 '06 #2
Hi Ingacio,
Just for your clarification, the "start" of the array is something
artificial based on the language used


This I get. In memory, two arrays that have a different base index but
the same content are really not different.

I feel the issue at hand is that I am working in a managed environment
where memory address is kept hidden ( I do not want to use "unsafe"
code). As a result, i have yet to find an elegant a solution.

Consider two int arrays (int[] x and int[] y) where x has a base index
of 1 and y has a base index of 0.

How do i convert from x to y?

Feb 8 '06 #3
Hi,

"AtariPete" <pc******@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Hi Ingacio, artificial based on the language used
This I get. In memory, two arrays that have a different base index but
the same content are really not different.
Nop, there is no thing like a base index, ALL arrays in C# start at index 0
I feel the issue at hand is that I am working in a managed environment
where memory address is kept hidden ( I do not want to use "unsafe"
code). As a result, i have yet to find an elegant a solution.
You do not need the memory address, when you receive your memory chunk from
the COM object it will be ready to be accessed as array[0] to get thr first
element.
Consider two int arrays (int[] x and int[] y) where x has a base index
of 1 and y has a base index of 0.


There is no thing as an array with base 1 in C# , once you accept this
everything will be clearer.

how are you receiving the array frmo the COM object? can you post the code ?

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Feb 8 '06 #4
>There is no thing as an array with base 1 in C# , once you accept this
everything will be clearer.
I know there base 1 arrays do not exist in C#. But they can be handled
in C#. No?
how are you receiving the array frmo the COM object?


I am using Microsoft's owc10 excel component. I am accessing a range of
data in an owc instance. This range is returned as an object. This
object is actually a 1 based object array (object[,]) of objects which
are actually strings. I am passing this data to a component that
required the array to be 0 based. This is why I need to convert from 0
based to 1 based.

Feb 8 '06 #5
Look into Buffer.BlockCopy. It's a lot faster than going through each item
and assigning it to another array.
"AtariPete" <pc******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
There is no thing as an array with base 1 in C# , once you accept this
everything will be clearer.


I know there base 1 arrays do not exist in C#. But they can be handled
in C#. No?
how are you receiving the array frmo the COM object?


I am using Microsoft's owc10 excel component. I am accessing a range of
data in an owc instance. This range is returned as an object. This
object is actually a 1 based object array (object[,]) of objects which
are actually strings. I am passing this data to a component that
required the array to be 0 based. This is why I need to convert from 0
based to 1 based.

Feb 8 '06 #6
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
This I get. In memory, two arrays that have a different base index but
the same content are really not different.


Nop, there is no thing like a base index, ALL arrays in C# start at index 0


Hmm... that's only sort of true.

1) You can certainly have instances of System.Array (rather than C#
arrays) with a non-zero base
2) You can have multidimensional arrays (created using
Array.CreateInstance) which can be cast to (say) int[,] and used as
normal, even with non-zero bases

It's all a little bit odd, unfortunately.

The C# specification claims that C# arrays (as opposed to .NET arrays)
are always 0-based, but as I say with multidimensional arrays that's
not always true...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 8 '06 #7
Jon Skeet [C# MVP] wrote:
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
This I get. In memory, two arrays that have a different base index but
the same content are really not different.


Nop, there is no thing like a base index, ALL arrays in C# start at index 0

Hmm... that's only sort of true.


In principle, I agree, Jon. But that's not what the OP originally
wanted. He said he had "(int[] x and int[] y)" which were ONE based
since they came from (for all intents and purposes) outside CSharp.
Ignacio's point is that the "base" of an int[] in CSharp is *always*
zero. So when the OP says that the array is ONE-based makes absolutely
no sense-- an array doesn't have a sense of "based"-ness.

I'm not sure I'm making myself understood-- I'll stop there.

Scott
Feb 9 '06 #8
Scott C <sdcoonce@g_m_a_i_l.com> wrote:
Nop, there is no thing like a base index, ALL arrays in C# start at index 0


Hmm... that's only sort of true.


In principle, I agree, Jon. But that's not what the OP originally
wanted. He said he had "(int[] x and int[] y)" which were ONE based
since they came from (for all intents and purposes) outside CSharp.
Ignacio's point is that the "base" of an int[] in CSharp is *always*
zero. So when the OP says that the array is ONE-based makes absolutely
no sense-- an array doesn't have a sense of "based"-ness.

I'm not sure I'm making myself understood-- I'll stop there.


An array does, but an int[] doesn't. An int[] is always zero-based, but
an int[,] isn't, and a System.Array isn't. I was only commenting for
the sake of precision, really :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 9 '06 #9
Jon Skeet [C# MVP] wrote:

An array does, but an int[] doesn't. An int[] is always zero-based, but
an int[,] isn't, and a System.Array isn't. I was only commenting for
the sake of precision, really :)

Just to beat a dead horse, an int[] isn't zero based, necessarily. In
CSharp it is, and in lots of other languages it is, but it's just a
convention. Kind of like calling 32°F freezing. The freezing point of
water is no more 32-indexed than int[] is zero indexed, it's just the
convention for a country or a compiler. (In this non-zero indexed
sense, I would say the USA has more in common with VB, and the rest of
the world is more akin to CS.)

The question would better be phrased as: given a list of numbers,
say 9, 4, 7, 1
how do we call these using an index. Since most would agree that it
makes sense that then _next_ number is index+1, the only real question
is how does the compiler "address" the number 9? --My point is that the
"list" doesn't change if 9 is addressed by 0, 2, 1.643 or 5.3e10.

OK, I think the horse is now sufficiently dead, I feel I can rest quietly.

Scott
Feb 10 '06 #10

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

Similar topics

9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
3
by: b83503104 | last post by:
In matlab, the sort function returns two things: =sort() a = 5 7 8 b = 1 3 2 where a is the sorted result, and b is the corresponding index. Is there C or C++ code...
5
by: danc | last post by:
I keep getting this error. I have scoured the web and could not find any applicable information. It seems to have really started since I upgraded to the 1.1 framework. Any help is appreciated. ...
2
by: Jess Andersen | last post by:
Hi there I have spent some time browsing for a solution to the following error but have unfortunatly not found any solution yet (the deleted parantheses means that i have removed the...
3
by: some one | last post by:
I have kind of wired problem, I using httpwebrequest to post form data to server , in the GetResponse stage a WebException occurred, after tracing the actual error that occurs on the server, I...
5
by: Peter Nofelt | last post by:
Hi all, My scenario is as follows: * Receive a base 1 array (index starts at 1 instead of 0) of data from a COM component . * Need to pass this array to a .net component that requires its...
4
by: kvicky | last post by:
I have multiline textbox and I am trying to do a search functionality based on the input entered in this Textbox. I am able to convert into a string array if the search components are separated by...
5
by: Maxim | last post by:
Hi all, I'm calling a COM Interface method that returnes SafeArray wrapped into variant. Is it possible to convert it to managed array? Because working with SAFEARRAY directly is a bit...
12
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} ...
2
by: selvamariappan | last post by:
Hi all , I am using encryption to encrypt the query string passed from one page to other. In works fine most of the time but occationally i get this error - The line that throws that...
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...
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)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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
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.