Click here to Skip to main content
15,886,077 members
Home / Discussions / C#
   

C#

 
QuestionHow to convert byte[] to int[]? Pin
Konstantin Vasserman7-Oct-02 3:49
Konstantin Vasserman7-Oct-02 3:49 
AnswerRe: How to convert byte[] to int[]? Pin
Paul Riley7-Oct-02 5:17
Paul Riley7-Oct-02 5:17 
GeneralRe: How to convert byte[] to int[]? Pin
Konstantin Vasserman7-Oct-02 5:38
Konstantin Vasserman7-Oct-02 5:38 
GeneralRe: How to convert byte[] to int[]? Pin
Paul Riley7-Oct-02 5:55
Paul Riley7-Oct-02 5:55 
GeneralRe: How to convert byte[] to int[]? Pin
Konstantin Vasserman7-Oct-02 6:07
Konstantin Vasserman7-Oct-02 6:07 
GeneralRe: How to convert byte[] to int[]? Pin
leppie7-Oct-02 7:20
leppie7-Oct-02 7:20 
GeneralRe: How to convert byte[] to int[]? Pin
Konstantin Vasserman7-Oct-02 7:48
Konstantin Vasserman7-Oct-02 7:48 
GeneralRe: How to convert byte[] to int[]? Pin
Richard Deeming7-Oct-02 7:42
mveRichard Deeming7-Oct-02 7:42 
This may not be the most efficient way of doing it, but it seems to work:
static Array ConvertByteArray(byte[] inArray, Type destType)
{
    if (!destType.IsValueType) 
        throw new Exception("destType must be a value type!");
 
    // Can't calculate size for enum types.
    if (destType.IsEnum) destType = Enum.GetUnderlyingType(destType);
 
    int iSize = System.Runtime.InteropServices.Marshal.SizeOf(destType);
 
    if (iSize > 8) 
        throw new Exception("destType is too large!");
 
    System.Diagnostics.Debug.Assert(
        inArray.Length % iSize == 0, 
        "Invalid array length!");
 
    Array outArray = Array.CreateInstance(
        destType, inArray.Length / iSize);
 
    for (int i=0, j=0; 
        i<inArray.Length && j<outArray.Length; 
        i += iSize, j++)
    {
        // The value will not be larger than a UInt64.
        // NB: Left-shift doesn't work on 64-bit numbers.
        ulong val = 0;
 
        // Bytes from source array
        for (int k=0; k<iSize && i+k < inArray.Length; k++)
            val = (val * 256) + inArray[i + k];
 
        // Shouldn't happen, but just in case
        for (int k=inArray.Length - i; k < iSize; k++)
            val *= 256;
 
        try 
        {
            outArray.SetValue(Convert.ChangeType(val, destType), j);
        }
        catch (OverflowException ex)
        {
            // If destType is signed, we may have a negative value
            ulong iMask = (ulong)Math.Pow(2, 8 * iSize - 1);
 
            if ((val & iMask) != 0)
            {
                // Try negative value.
                // NB: If destType is long, (long)iMask is negative
                // so just cast val to long.
                long val2;
                if ((long)iMask < 0) 
                    val2 = (long)val;
                else 
                    val2 = (long)(val & ~iMask) - (long)iMask;
 
                outArray.SetValue(Convert.ChangeType(val2, destType), j);
            }
            else
                // Not negative - just allow the 
                // exception to propogate
                throw ex;
        }
    }
 
    return outArray;
}

If you know the type at compile-time, you can use:
byte[] a = new byte[6] {12, 34, 56, 78, 91, 111};
short[] b = (short[])ConvertByteArray(a, typeof(short));

This code will test the function:
static void TestConvert(Type T)
{
    int tSize;
    if (T.IsEnum) 
        tSize = System.Runtime.InteropServices.Marshal.SizeOf(
            Enum.GetUnderlyingType(T));
    else 
        tSize = System.Runtime.InteropServices.Marshal.SizeOf(T);
 
    Random R = new Random();
 
    int iBytes = R.Next(1, 10) * tSize;
    Console.WriteLine("{0} bytes", iBytes);
 
    byte[] a = new byte[iBytes];
 
    for (int i=0; i<a.Length; i++)
    {
        a[i] = (byte)R.Next(255);
        Console.WriteLine("a[{0}] = {1}", i, a[i]);
    }
 
    Array b = ConvertByteArray(a, T);
 
    for (int i=0; i<b.Length; i++) 
        Console.WriteLine("b[{0}] = {1}", i, b.GetValue(i));
}
 
[Flags]
public enum TestEnum : byte
{
    Value0 = 1,
    Value1 = 2,
    Value2 = 4,
    Value3 = 8,
    Value4 = 16,
    Value5 = 32,
    Value6 = 64,
    Value7 = 128,
}
 
static void Main()
{
    TestConvert(typeof(byte));
    TestConvert(typeof(sbyte));
    TestConvert(typeof(short));
    TestConvert(typeof(ushort));
    TestConvert(typeof(int));
    TestConvert(typeof(uint));
    TestConvert(typeof(long));
    TestConvert(typeof(ulong));
 
    // NB: This will be the same as byte
    TestConvert(typeof(TestEnum));
 
    // Early-bound enum test
    Random R = new Random();
    byte[] a = new byte[4];
    for (int i=0; i<4; i++)
    {
        a[i] = (byte)R.Next(255);
        Console.WriteLine("a[{0}] = {1}", i, a[i]);
    }
 
    TestEnum[] b = (TestEnum[])ConvertByteArray(a, typeof(TestEnum));
    for (int i=0; i < b.Length; i++)
        Console.WriteLine("b[{0}] = {1}", i, b[i]);
}

GeneralRe: How to convert byte[] to int[]? Pin
Konstantin Vasserman7-Oct-02 7:55
Konstantin Vasserman7-Oct-02 7:55 
GeneralDataGrid Updates Pin
Dave Moor7-Oct-02 1:25
Dave Moor7-Oct-02 1:25 
QuestionHow to use scripts in my applicaction?!? Pin
EdgarBM6-Oct-02 23:58
EdgarBM6-Oct-02 23:58 
GeneralAspose.Obfuscator 1.0 Released! Pin
Aspose Pty Ltd6-Oct-02 21:31
Aspose Pty Ltd6-Oct-02 21:31 
GeneralRe: Aspose.Obfuscator 1.0 Released! Pin
Stephane Rodriguez.6-Oct-02 22:19
Stephane Rodriguez.6-Oct-02 22:19 
GeneralRe: Aspose.Obfuscator 1.0 Released! Pin
leppie7-Oct-02 8:24
leppie7-Oct-02 8:24 
GeneralRe: Aspose.Obfuscator 1.0 Released! Pin
Patrick Lassalle14-Oct-02 12:01
Patrick Lassalle14-Oct-02 12:01 
Questionsetup wizered? Pin
imran_rafique6-Oct-02 17:45
imran_rafique6-Oct-02 17:45 
GeneralC# Debug problem in VS7 Pin
Elf6-Oct-02 16:45
Elf6-Oct-02 16:45 
GeneralRe: C# Debug problem in VS7 Pin
Russell Morris7-Oct-02 6:28
Russell Morris7-Oct-02 6:28 
GeneralVideo Streaming Pin
yccheok6-Oct-02 16:13
yccheok6-Oct-02 16:13 
GeneralRe: Video Streaming Pin
Daniel Strigl6-Oct-02 20:12
Daniel Strigl6-Oct-02 20:12 
GeneralRe: Video Streaming Pin
yccheok6-Oct-02 20:16
yccheok6-Oct-02 20:16 
GeneralRe: Video Streaming Pin
Philip Fitzsimons7-Oct-02 3:21
Philip Fitzsimons7-Oct-02 3:21 
GeneralEditing Bitmap File When PictureBox.Image Point To It Pin
yccheok6-Oct-02 15:31
yccheok6-Oct-02 15:31 
Generalproblem on drawing theme background on a bitmap Pin
Li-kai Liu (Angus)6-Oct-02 7:28
Li-kai Liu (Angus)6-Oct-02 7:28 
Questionhow to uninstall a software programatically ? Pin
imran_rafique5-Oct-02 16:49
imran_rafique5-Oct-02 16:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.