将浮点向量重新解释为无符号char数组并返回 [英] Reinterpret float vector as unsigned char array and back

查看:51
本文介绍了将浮点向量重新解释为无符号char数组并返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索并搜索了stackoverflow来找到答案,但是没有找到我所需要的.

I've searched and searched stackoverflow for the answer, but have not found what I needed.

我有一个例程,该例程将无符号char数组作为参数,以便将其编码为Base64.我想在Base64中对STL浮点向量(向量)进行编码,因此需要将浮点向量中的字节重新解释为无符号字符数组,以便将其传递给编码例程.我已经尝试了许多方法,从重新解释和静态强制转换到内存副本等,但是似乎都没有用(至少不是我实现它们的方式).

I have a routine that takes an unsigned char array as a parameter in order to encode it as Base64. I would like to encode an STL float vector (vector) in Base64, and therefore would need to reinterpret the bytes in the float vector as an array of unsigned characters in order to pass it to the encode routine. I have tried a number of things from reinterpret and static casts, to mem copies, etc, but none of them seem to work (at least not the way I implemented them).

同样,在将编码后的数据解码回float数组时,我需要做完全相反的事情.解码例程会将解码后的数据提供为无符号char数组,我将需要重新解释该字节数组,然后再次将其转换为浮点向量.

Likewise, I'll need to do the exact opposite when decoding the encoded data back to a float array. The decode routine will provide the decoded data as an unsigned char array, and I will need to reinterpret that array of bytes, converting it to a float vector again.

这是我的C ++代码的精简版,可以进行编码:

Here is a stripped down version of my C++ code to do the encoding:

std::string
EncodeBase64FloatVector( const vector<float>& p_vector )
{
  unsigned char* sourceArray;

  // SOMEHOW FILL THE sourceArray WITH THE FLOAT VECTOR DATA BITS!!

  char* target;
  size_t targetSize = p_vector.size() * sizeof(float);
  target = new char[ targetSize ];

  int result = EncodeBase64( sourceArray, floatArraySizeInUChars, target, targetSize );

  string returnResult;
  if( result != -1 )
  {
    returnResult = target;
  }
  delete target;
  delete sourceArray;
  return returnResult;
}

任何帮助将不胜感激.谢谢.

Any help would be greatly appreciated. Thanks.

雷蒙德.

推荐答案

std :: vector 保证数据将是连续的,并且可以通过获取指向向量中第一个元素的指针第一个元素的地址(假设它不为空).

std::vector guarantees the data will be contiguous, and you can get a pointer to the first element in the vector by taking the address of the first element (assuming it's not empty).

typedef unsigned char byte;
std::vector<float> original_data;
...
if (!original_data.empty()) {
  const float *p_floats = &(original_data[0]);  // parens for clarity

现在,要将其视为 unsigned char 的数组,请使用 reinterpret_cast :

Now, to treat that as an array of unsigned char, you use a reinterpret_cast:

  const byte *p_bytes = reinterpret_cast<const byte *>(p_floats);
  // pass p_bytes to your base-64 encoder
}

您可能想在其余数据之前对向量的长度进行编码,以使其更易于解码.

You might want to encode the length of the vector before the rest of the data, in order to make it easier to decode them.

注意:您仍然必须担心字节顺序和表示形式的详细信息.仅当您在与您使用的同一平台(或兼容平台)上进行读回时,此方法才有效.

CAUTION: You still have to worry about endianness and representation details. This will only work if you read back on the same platform (or a compatible one) that you wrote with.

这篇关于将浮点向量重新解释为无符号char数组并返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆