I try to learn F# and at this time i try to convert a simple C# Function to F#
in some points i stuck at example var compressedData = new byte[memoryStream.Length]; < C#
here the full c# function
public static string CompressString(string text)
{
byte[] buffer = Encoding.UTF8.GetBytes(text);
var memoryStream = new MemoryStream();
using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
{
gZipStream.Write(buffer, 0, buffer.Length);
}
memoryStream.Position = 0;
var compressedData = new byte[memoryStream.Length];
memoryStream.Read(compressedData, 0, compressedData.Length);
var gZipBuffer = new byte[compressedData.Length + 4];
Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length);
Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4);
return Convert.ToBase64String(gZipBuffer);
}
and here Mine half Translated version
let compress(text:string)=
let buffer = Encoding.UTF8.GetBytes(text)
use memoryStream = new MemoryStream()
let gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true)
gZipStream.Write(buffer, 0, buffer.Length)
memoryStream.Position <- Convert.ToInt64(0)
let compressedData = Array.init memoryStream.Length(fun i -> byte) //< Here i stuck
memoryStream.Read(compressedData, 0, compressedData.Length)
use gZipBuffer = (compressedData.Length + 4)
the last three days i have searched on google to solve my problem, but i didnt find any solution.
I hope someone here can help me 🙂
Thank you very much in advance
You’re looking for
Array.zeroCreate.