英文:
How to convert byte array containing float values to a double array in c#
问题
以下是翻译好的代码部分:
public static void Test()
{
var floatArray = new float[] { 123.45f, 123f, 45f, 1.2f, 34.5f };
// 创建一个字节数组并将浮点数复制到其中...
var byteArray = new byte[floatArray.Length * sizeof(float)];
Buffer.BlockCopy(floatArray, 0, byteArray, 0, byteArray.Length);
// 创建一个双精度浮点数数组并将字节复制到其中...
var doubleArray = new double[byteArray.Length / 4];
Buffer.BlockCopy(byteArray, 0, doubleArray, 0, byteArray.Length);
// 我们是否有与开始时相同的浮点数序列?
foreach(var i in doubleArray)
{
Console.WriteLine(i);
// 输出 --> 387028163194470.4 , 0.02500000981399353 , 5.474008307E-315 , 0 , 0
}
}
希望这有所帮助。
英文:
I want to convert a byte array containing floats(4 bytes as a float) into a double array in the fastest way possible so I'm using blockcopy as internet suggested it. But , it seems like the values were not correct after the conversion into double can somebody help with!
public static void Test()
{
var floatArray = new float[] { 123.45f, 123f, 45f, 1.2f, 34.5f };
// create a byte array and copy the floats into it...
var byteArray = new byte[floatArray.Length * sizeof(float)];
Buffer.BlockCopy(floatArray, 0, byteArray, 0, byteArray.Length);
// create a double array and copy the bytes into it...
var doubleArray = new double[byteArray.Length / 4];
Buffer.BlockCopy(byteArray, 0, doubleArray, 0, byteArray.Length);
// do we have the same sequence of floats that we started with?
foreach(var i in doubleArray)
{
Console.WriteLine(i);
// outputs --> 387028163194470.4 , 0.02500000981399353 , 5.474008307E-315 , 0 , 0
}
}
答案1
得分: 2
public static void Test()
{
float val = 1024.24f;
unsafe
{
// byte buffer
Span
// float to bytes
fixed (byte* ptr = bytes) *((float*)ptr) = val;
// bytes to double
double dbl = default;
fixed (byte* ptr = bytes) dbl = *((float*)ptr);
// result
Console.Write(dbl);
//output: 1024.239990234375d == 1024.24f
}
}
See instructions below to understand what's happening on here - . The bytes are loaded onto the stack as a Float32, top of stack is converted to Float64, and then it's popped off the stack into the local double
at index 2...
英文:
If you need speed, you're not going to get faster than this...
public static void Test()
{
float val = 1024.24f;
unsafe
{
// byte buffer
Span<byte> bytes = stackalloc byte[Unsafe.SizeOf<double>()];
// float to bytes
fixed (byte* ptr = bytes) *((float*)ptr) = val;
// bytes to double
double dbl = default;
fixed (byte* ptr = bytes) dbl = *((float*)ptr);
// result
Console.Write(dbl);
//output: 1024.239990234375d == 1024.24f
}
}
See instructions below to understand what's happening on here - . The bytes are loaded onto the stack as a Float32, top of stack is converted to Float64, and then it's popped off the stack into into the local double
at index 2...
答案2
得分: 1
Array.Copy()
可以将 float
转换为 double
。
以下是一个示例:
static class Program
{
static void Main(string[] args)
{
float[] f_arr = new float[] { 1.1111f, 22.22f, 333.3f };
Console.WriteLine(string.Join(", ", f_arr));
double[] d_arr = new double[f_arr.Length];
Array.Copy(f_arr, d_arr, f_arr.Length);
Console.WriteLine(string.Join(", ", d_arr));
}
}
输出结果如下:
1.1111, 22.22, 333.3
1.1110999584198, 22.2199993133545, 333.299987792969
正如您所看到的,精度有所下降(因为float
的 24 位尾数被赋给了 double
的 53 位尾数)。但这样做可以完成任务,而无需担心使用字节数组和Buffer.Copy()
。
英文:
Array.Copy()
can convert from float
to double
.
Here is an example
static class Program
{
static void Main(string[] args)
{
float[] f_arr = new float[] { 1.1111f, 22.22f, 333.3f };
Console.WriteLine(string.Join(", ", f_arr));
double[] d_arr = new double[f_arr.Length];
Array.Copy(f_arr, d_arr, f_arr.Length);
Console.WriteLine(string.Join(", ", d_arr));
}
}
with output:
1.1111, 22.22, 333.3
1.1110999584198, 22.2199993133545, 333.299987792969
as you can see the precision is crap (since the 24 bit mantissa of float
is assigned to the 53 bit mantissa of double
). But it will do the trick without worrying about using byte arrays, and Buffer.Copy()
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论