Untested example:

C#

// byte[] data is the input // dataWidth is bits per sample (must be multiple of 8 and not larger than 64) // bytes per sample int bytes_per_sample = dataWidth / 8; // Optional supporting dataWidth not being a multiple of 8 //if (dataWidth % 8) // bytes_per_sample++; // total number of samples int num_samples = data.Length / bytes_per_sample; UInt64[] samples = new UInt64[num_samples]; for (int i = 0; i < num_samples; i++) { // Assign the lowest byte (the only with 8 bit samples) samples[i] = data[i * bytes_per_sample]; // Add the remaining bytes each shifted left by multiples of 8 for (int j = 1, uint shift = 8; j < bytes_per_sample; j++, shift += 8) { samples[i] |= data[i * bytes_per_sample + j] << shift; } }Note that the above assumes the first bytes in the input stream are the lower bytes. If that does not apply, the above code has to be modified accordingly.