将无符号字符数组转换为浮点数数组
在 C++ 中将无符号字符数组转换为浮点数组的最佳方法是什么?
我目前有一个 for 循环,如下
for (i=0 ;i< len; i++)
float_buff[i]= (float) char_buff[i];
我还需要反转该过程,即从无符号字符转换为浮点(浮点到 8 位转换)
for (i=0 ;i< len; i++)
char_buff[i]= (unsigned char) float_buff[i];
任何建议将不胜感激
谢谢
What is the best way of converting a unsigned char array to a float array in c++?
I presently have a for loop as follows
for (i=0 ;i< len; i++)
float_buff[i]= (float) char_buff[i];
I also need to reverse the procedure, i.e convert from unsigned char to float (float to 8bit conversion)
for (i=0 ;i< len; i++)
char_buff[i]= (unsigned char) float_buff[i];
Any advice would be appreciated
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我认为最好的方法是使用函数对象:
后面跟着:
这是最具可读性的,因为它用英语说明了正在执行的操作:使用静态转换将序列转换为不同的类型。 未来的演员阵容可以在一行中完成。
I think the best way is to use a function object:
followed by:
This is the most readable because it says what is being done in English: transforming a sequence into a different type using static casting. And future casts can be done in one line.
您的解决方案几乎是最好的选择,但是,我会考虑切换到:
Your solution is pretty much the best option, however, I would consider switching to:
转换是自动的,因此您无需明确说明。
但您可以使用标准算法:
从 float 转换回 char 可能会丢失信息。 所以你需要更明确。
这里我们使用 MyTransform 类,它应该有一个接受 float 并返回 char 的operator()。 这应该是微不足道的暗示。
The cast is automatic so you don't need to make it explicit.
But you can use the standard algorithms:
Converting back from float to char there is a potential loss of information. So you need to be more explicit.
Here we use the class MyTransform which should have an operator() that takes a float and returns a char. That should be trivial to impliment.
您的解决方案似乎是正确的,尽管在返回的过程中,您可能会丢失铸造中的浮动数字。
Your solution seems right, though on the way back, you might lose the floating digits in the casting.
你这样做的目的是什么? 将浮点数推入字符中实际上没有意义。 在大多数平台上,float 为 4 个字节,表示浮点数,而 char 为 1 个字节,通常表示单个字符。 尝试将 float 推入 char 时会丢失 3 个字节的数据,对吧?
For what purpose are you doing this? Shoving a float into a char doesn't really make sense. On most platforms a float will be 4 bytes and represent a floating point number, where as a char will be 1 byte and often represents a single character. You'll lose 3 bytes of data trying to shove a float into a char, right?
您的第一个循环不需要强制转换。 您可以隐式地从一种类型(例如,
unsigned char
)转换为更广泛的类型(例如,float
)。 您的第二个循环应使用static_cast
:我们使用
static_cast
显式告诉编译器转换为更窄的类型。 如果您不使用强制转换,编译器可能会警告您转换可能会丢失数据。 强制转换运算符的存在意味着您了解可能会丢失数据精度,并且您对此表示同意。 这不不适合使用reinterpret_cast
。 使用static_cast
,您至少对可以进行的转换有一些限制(例如,它可能不允许您将Bird*
转换为Nuclear_Submarine* )。
reinterpret_cast
没有这样的限制。另外,以下是 Bjarne Stroustrup 对此的看法主题。
Your first loop doesn't require a cast. You can implicitly convert from one type (e.g.,
unsigned char
) to a wider type (e.g.,float
). Your second loop should usestatic_cast
:We use
static_cast
to explicitly tell the compiler to do the conversion to a narrower type. If you don't use the cast, your compiler might warn you that the conversion could lose data. The presence of the cast operator means that you understand you might lose data precision and you're ok with it. This is not an appropriate place to usereinterpret_cast
. Withstatic_cast
, you at least have some restrictions on what conversions you can do (e.g., it probably won't let you convert aBird*
to aNuclear_Submarine*
).reinterpret_cast
has no such restrictions.Also, here's what Bjarne Stroustrup has to say about this subject.
如果您正在处理非常大的数组并且性能至关重要,那么以下方法可能会稍微更有效:
If you are dealing with very large arrays and performance is essential then the following may prove slightly more efficient:
没有人提到过这一点,但是如果您正在对浮点数进行任何算术运算,您可能需要舍入而不是截断...如果您有 char 49,并且它会变成 4.9E1,经过一些算术之后,它可能会变成 4.89999999E1,当你把它转回 char 时,它会变成 48。
如果你没有对浮动做任何事情,这应该不是问题,但是为什么你首先需要它作为浮动呢?
No one has mentioned this, but if you're doing any arithmetic with the floats, you may want to round instead of truncate... if you have the char 49, and it gets turned into 4.9E1, after some arithmetic, it might turn into 4.89999999E1 which will turn into 48 when you turn it back into a char.
If you're not doing anything with the float, this shouldn't be a problem, but then why do you need it as a float in the first place?