![]() |
| | #1 |
| Registered User Join Date: Sep 2008
Posts: 4
| C# Converting one datatype array to another I compared using a for loop vs the Array.Copy() and there is a sigficant optimization when using the Array.Copy. Now I am trying to see if there is a similar function that you can use on more unique datatypes? The code I currently use to convert my 12 bit data into an integer is as follow: Code: index2 = 0;
for(index = 0; index < 230; index+=2)
{
ecg_buffer[index] = (temp_buffer[index2] << 4) | ((temp_buffer[index2+1] & 0xF0)>> 4);
ecg_buffer[index+1] = ((temp_buffer[index2+1] & 0x0F) << 8) | temp_buffer[index2+2];
index2+=3;
}
ecg_buffer contain the converted integer values Now is there an optimized function in CSharp that will convert every 12 bits to another datatype array? Thank you much, Kevin |
| kevins963 is offline | |
| | #2 |
| uh oh Join Date: Jan 2005 Location: Ontario, CA
Posts: 66
| You can look into the BitConverter class and its methods, for converting specific numbers of bits might just mean bitshifting values in order to obtain the appropriate information before passing it to the appropriate method (in the case of your provided example, ToIntxx() depending on the end resulting data Type you desire. It basically provides methods for almost every built in type. Hope this helps. I'm looking for either a one method conversion, or a method to make one of my own. I'll update this thread if I find anything else in my own search. [EDIT]Check out System.BitConverter Members for more info on what I referenced above.[/EDIT] Last edited by cyreon; 09-13-2009 at 08:54 PM. |
| cyreon is offline | |
| | #3 |
| Guest Join Date: Aug 2001
Posts: 5,034
| You could use System.BitConverter, as cyreon suggested, and process 24 bits at a time, eg: Code: using System;
class Test
{
static void Main( )
{
byte [ ]
data = new byte[ ]
{
0x12,
0x34,
0x56,
0x12,
0x34,
0x56,
0x12,
0x34,
0x56,
0x12,
0x34,
0x56,
/*
Padding, to prevent access violation
*/
0x00
};
/*
Special calculation needed due to padding
*/
int
limit = data.Length - ( data.Length % 3 );
for( int index = 0; index < limit; index += 3 )
{
int
buffer24 = BitConverter.ToInt32( data, index ) & 0x00ffffff,
first12 = buffer24 & 0xfff,
second12 = ( buffer24 >> 12 );
Console.Write( "first12 : 0x" + Convert.ToString( first12, 16 ).PadLeft( 3, '0' ) + "\n" );
Console.Write( "second12: 0x" + Convert.ToString( second12, 16 ).PadLeft( 3, '0' ) + "\n" );
/*
Put first12 and second12 into an array or such
*/
}
}
}
|
| Sebastiani is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [question]Analyzing data in a two-dimensional array | burbose | C Programming | 2 | 06-13-2005 07:31 AM |
| Type and nontype parameters w/overloading | Mr_LJ | C++ Programming | 3 | 01-02-2004 01:01 AM |
| two dimensional dynamic array? | ichijoji | C++ Programming | 6 | 04-14-2003 04:27 PM |
| converting an integar into an array? | The Gweech | C++ Programming | 15 | 07-30-2002 04:44 PM |
| Help with an Array | omalleys | C Programming | 1 | 07-01-2002 08:31 AM |