在缓冲器1和缓冲器的值不能是相同的或者它会工作。也许你在printf命令中使用的格式掩盖了差异(%i,%f等)。不要使用printf,请尝试设置断点并使用调试器查看值。这可能有助于揭示实际发生了什么问题。
编辑:
鉴于你关于你如何执行转换的意见,我觉得我现在可以提供帮助。原始数据是unsigned char类型。在大多数平台上,这将是一个介于0和255之间的整数值。您希望将此值转换为浮点数来执行操作。为了使数据对于任何操作都具有浮点类型的意义,您希望将该范围缩放到+/- 1.0之间。这是以下代码中“scale”变量的用处。
#include <iostream>
#include <math.h>
int main()
{
const int BUFFER_LEN = 6;
const unsigned char channelDataIN[] = {0,255, 1, 254, 2, 253};
unsigned char channelDataOUT[BUFFER_LEN];
float channelDataF[BUFFER_LEN];
std::cout.precision(5);
float scale = powf(2.f, 8.f*sizeof(unsigned char)) - 1.f;
for (int mm = 0; mm < BUFFER_LEN; ++mm)
{
std::cout << "Original = " << (int)channelDataIN[mm] << std::endl;
channelDataF[mm] = (float)(channelDataIN[mm]) * 2.f/scale - 1.f; //Float cast
std::cout << "Float conversion = " << channelDataF[mm] << std::endl;
channelDataOUT[mm] = (unsigned char) ceil( (1.f+channelDataF[mm]) * scale/2.f );
std::cout << "Recovered = " << (int)channelDataOUT[mm] << std::endl;
if (channelDataIN[mm] == channelDataOUT[mm])
std::cout << "The output precisely equals the input" << std::endl << std::endl;
else
std::cout << "The output != input" << std::endl << std::endl;
}
return 0;
}
将值转换回来后的无符号字符输出数组与输入数组相同。这是代码的输出。 。 。
Original = 0
Float conversion = -1
Recovered = 0
The output precisely equals the input
Original = 255
Float conversion = 1
Recovered = 255
The output precisely equals the input
Original = 1
Float conversion = -0.99216
Recovered = 1
The output precisely equals the input
Original = 254
Float conversion = 0.99216
Recovered = 254
The output precisely equals the input
Original = 2
Float conversion = -0.98431
Recovered = 2
The output precisely equals the input
Original = 253
Float conversion = 0.98431
Recovered = 253
The output precisely equals the input
你好,谢谢你的回答。我在古典中扮演角色,我认为:for(i = 0; i
victor
2012-03-30 08:12:06
当我比较buffer1 [i]和buffer [2]值时,它们不相等(buffer2 [i]大于buffer2 [i]) – victor 2012-03-30 08:57:16