Created
September 19, 2023 08:23
-
-
Save chgeuer/bef1976772ee678583339aebbabf8b8b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unsafe static byte[] DoublesToBytes(double[] value) | |
{ | |
var bytes = new byte[8*value.Length]; | |
fixed (byte* b = bytes) | |
{ | |
for (int i = 0; i < value.Length; i++) | |
{ | |
var v = value[i]; | |
*((int*)(b + 8*i)) = *(int*)&v; | |
*((int*)(b + 8*i+4)) = *(((int*)&v) + 1); | |
*((double*)(b + i*8)) = value[i]; | |
} | |
} | |
return bytes; | |
} | |
static unsafe double[] BytesToDoubles(byte[] data) | |
{ | |
var result = new double[data.Length / 8]; | |
fixed (byte* ptr = &data[0]) | |
{ | |
for (var i= 0; i < result.Length; i++) | |
{ | |
result[i] = *((double*)(ptr + i * 8)); | |
} | |
} | |
return result; | |
} | |
var f = new[] { 1.23399996758d, 3.14, 0 }; | |
byte[] bytes = DoublesToBytes(f); | |
Console.WriteLine(BitConverter.ToString(bytes)); | |
foreach (var decoded in BytesToDoubles(bytes)) | |
{ | |
Console.WriteLine(decoded); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment