Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an integer array of length 900 that contains only binary data [0,1]. I want to short the length of the array without losing binary data formate(original array values).
Is it possible to short the length of array of 900 into 10 or 20 length in C#???
Posted

1 solution

No.
There is no re-dimension facility in C#.
Instead, create a new array and use Array.Copy[^] to transfer the original values.
private T[] shrinkArray<T>(T[] oldArray, int newLength)
    {
    T[] newArray = new T[newLength];
    Array.Copy(oldArray, newArray, newLength);
    return newArray;
    }



"But it loses original data in array of 900 length. What you say now?"

Oops! Sorry - I missed that you were trying to pack the bits!

Try this: (untested)
C#
private static int[] PackBits(int[] inp)
    {
    int intsRequired = (inp.Length + 31) / 32;
    int[] outp = new int[intsRequired];
    int curOut = 0;
    for (int i = 0; i < inp.Length; i++)
        {
        outp[curOut] = (outp[curOut] << 1) | inp[i];
        if ((i % 32) == 31)
            {
            // output int complete
            curOut++;
            }
        }
    return outp;
    }




"Well, it works fine. Thanks :)
Now,my next question is how to unpack it (get original back)????"




Well, the Haynes Book of Lies always suggests that "reassembly is the reverse of disassembly".

I used to hate that! But it is true.

I have revised the PackBits so it is easier to unpack when the data doesn't fit exactly into a complete int:

C#
private static int[] PackBits(int[] inp)
    {
    int intsRequired = (inp.Length + 31) / 32;
    int[] outp = new int[intsRequired];
    int curOut = 0;
    for (int i = 0; i < inp.Length; i++)
        {
        outp[curOut] |= inp[i] << (i % 32);
        if ((i % 32) == 31)
            {
            // output int complete
            curOut++;
            }
        }
    return outp;
    }
private static int[] UnpackBits(int[] inp, int len)
    {
    int intsRequired = inp.Length * 32;
    int[] outp = new int[intsRequired];
    int curOut = 0;
    foreach (int val in inp)
        {
        uint x = (uint) val;
        for (int i = 0; i < 32; i++)
            {
            outp[curOut++] = (int) ((x >> i) & 1);
            }
        }
    return outp;
    }
 
Share this answer
 
v3
Comments
Espen Harlinn 6-Feb-11 6:47am    
Good answer :) nice and simple
mirfan00 6-Feb-11 6:57am    
But it loses original data in array of 900 length. What you say now?
OriginalGriff 6-Feb-11 7:11am    
Sorry about that - answer updated
mirfan00 6-Feb-11 7:24am    
Well, it works fine. Thanks :)
Now,my next question is how to unpack it (get original back)????
OriginalGriff 6-Feb-11 7:53am    
"Reassembly is the reverse of disassembly" :laugh:

Answer updated!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900