我有一个包含二进制格式数字的 char 数组。数字采用以下格式:整数、整数和双精度数。在 C++ 中将 char 数组转换为整数数组和双精度数组的最佳和最快方法是什么?现在我正在使用以下代码,它工作正常,但我认为它不够高效!
#define SIZE 5000
char chBuf[SIZE]; // contains double numbers in binary format
char dBuf[sizeof(double)];
char IBuf[sizeof(int)];
int count =0;
for (int i=0; i<numberofdata; i++)
{
for (int j=0; j<4; j++) IBuf[j] = chBuf[j + count];
Integer[i] = *(integer*) (IBuf);
count += sizeof(integer);
for (int j=0; j<4; j++) IBuf[j] = chBuf[j + count];
Integer[i+1] = *(integer*) (IBuf);
count += sizeof(integer);
for (int j=0; j<8; j++) dBuf[j] = chBuf[j + count];
Double[i] = *(double*) (dBuf);
count += sizeof(double);
}
我认为复制部分到 dBuf[] 和 IBuf[] 不应该是必要的,但我不知道如何摆脱它们。