C# Converting one datatype array to another

This is a discussion on C# Converting one datatype array to another within the C# Programming forums, part of the General Programming Boards category; I am trying to find a fast way to convert a byte array that stores 12 bit data into an ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    4

    C# Converting one datatype array to another

    I am trying to find a fast way to convert a byte array that stores 12 bit data into an integer array in CSharp.

    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;
    }
    temp_buffer is a byte array that holds the 12 bit values, so every three bytes contain two 12-bit values.

    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

  2. #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.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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
            */
            }
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  2. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  3. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM
  4. converting an integar into an array?
    By The Gweech in forum C++ Programming
    Replies: 15
    Last Post: 07-30-2002, 04:44 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21