将无符号字符数组转换为浮点数数组

发布于 2024-07-19 15:30:22 字数 330 浏览 9 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

戏蝶舞 2024-07-26 15:30:22

我认为最好的方法是使用函数对象:

template <typename T> // T models Any
struct static_cast_func
{
  template <typename T1> // T1 models type statically convertible to T
  T operator()(const T1& x) const { return static_cast<T>(x); }
};

后面跟着:

std::transform(char_buff, char_buff + len, float_buff, static_cast_func<float>());
std::transform(float_buff, float_buff + len, char_buff, static_cast_func<unsigned char>());

这是最具可读性的,因为它用英语说明了正在执行的操作:使用静态转换将序列转换为不同的类型。 未来的演员阵容可以在一行中完成。

I think the best way is to use a function object:

template <typename T> // T models Any
struct static_cast_func
{
  template <typename T1> // T1 models type statically convertible to T
  T operator()(const T1& x) const { return static_cast<T>(x); }
};

followed by:

std::transform(char_buff, char_buff + len, float_buff, static_cast_func<float>());
std::transform(float_buff, float_buff + len, char_buff, static_cast_func<unsigned char>());

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.

℡寂寞咖啡 2024-07-26 15:30:22

您的解决方案几乎是最好的选择,但是,我会考虑切换到:

char_buff[i]= static_cast<unsigned char>(float_buff[i]);

Your solution is pretty much the best option, however, I would consider switching to:

char_buff[i]= static_cast<unsigned char>(float_buff[i]);
爺獨霸怡葒院 2024-07-26 15:30:22

转换是自动的,因此您无需明确说明。
但您可以使用标准算法:

std::copy(char_buff,char_buff+len,float_buff);

从 float 转换回 char 可能会丢失信息。 所以你需要更明确。

std::transform(float_buff,float_buff+len,char_buff,MyTransform());

这里我们使用 MyTransform 类,它应该有一个接受 float 并返回 char 的operator()。 这应该是微不足道的暗示。

The cast is automatic so you don't need to make it explicit.
But you can use the standard algorithms:

std::copy(char_buff,char_buff+len,float_buff);

Converting back from float to char there is a potential loss of information. So you need to be more explicit.

std::transform(float_buff,float_buff+len,char_buff,MyTransform());

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.

你曾走过我的故事 2024-07-26 15:30:22

您的解决方案似乎是正确的,尽管在返回的过程中,您可能会丢失铸造中的浮动数字。

Your solution seems right, though on the way back, you might lose the floating digits in the casting.

吃颗糖壮壮胆 2024-07-26 15:30:22

你这样做的目的是什么? 将浮点数推入字符中实际上没有意义。 在大多数平台上,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?

下雨或天晴 2024-07-26 15:30:22

您的第一个循环不需要强制转换。 您可以隐式地从一种类型(例如,unsigned char)转换为更广泛的类型(例如,float)。 您的第二个循环应使用 static_cast

for (i=0; i< len; i++)
    char_buff[i]= static_cast<unsigned char>(float_buff[i]);

我们使用 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 use static_cast:

for (i=0; i< len; i++)
    char_buff[i]= static_cast<unsigned char>(float_buff[i]);

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 use reinterpret_cast. With static_cast, you at least have some restrictions on what conversions you can do (e.g., it probably won't let you convert a Bird* to a Nuclear_Submarine*). reinterpret_cast has no such restrictions.

Also, here's what Bjarne Stroustrup has to say about this subject.

东风软 2024-07-26 15:30:22

如果您正在处理非常大的数组并且性能至关重要,那么以下方法可能会稍微更有效:

   float *dst = float_buff;
   unsigned char *src = char_buff;

   for (i=0; i<len; i++) *dst++ = (float)*src++;

If you are dealing with very large arrays and performance is essential then the following may prove slightly more efficient:

   float *dst = float_buff;
   unsigned char *src = char_buff;

   for (i=0; i<len; i++) *dst++ = (float)*src++;
迟到的我 2024-07-26 15:30:22

没有人提到过这一点,但是如果您正在对浮点数进行任何算术运算,您可能需要舍入而不是截断...如果您有 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文