Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将浮点转换为向量<;无符号字符>;在C++;?_C++_Vector - Fatal编程技术网

C++ 如何将浮点转换为向量<;无符号字符>;在C++;?

C++ 如何将浮点转换为向量<;无符号字符>;在C++;?,c++,vector,C++,Vector,我读过,但它并没有帮助我解决我的问题。 我想将std::vector转换回float。unsigned char*bytes=&(readRequestArray)上的行不工作,上面的行仅打印字节。如何将其转换回浮点数 class HCSR04: public ISensor { public: HCSR04(); HCSR04(int trigger, int echo); ~HCSR04(); float distanceCentimeters();

我读过,但它并没有帮助我解决我的问题。 我想将
std::vector
转换回
float
unsigned char*bytes=&(readRequestArray)上的行不工作,上面的行仅打印字节。如何将其转换回浮点数

class HCSR04: public ISensor {
public:
    HCSR04();
    HCSR04(int trigger, int echo);
    ~HCSR04();
    float distanceCentimeters();
    std::vector<unsigned char> readRequest();
}

std::vector<unsigned char> HCSR04::readRequest() {
    float preCent = distanceCentimeters();
    const unsigned char* bytes = reinterpret_cast<const unsigned char*>(&preCent);
    std::vector<unsigned char> buffer(bytes, bytes + sizeof(float));
    for (int j = 0; j < buffer.size(); j++) {
        std::cout << buffer[j];
    }
    std::cout << std::endl;
    return buffer;
}

int main(void) {
    std::vector<unsigned char> readRequestArray = sensorUltrasonic->readRequest();
        for (int j = 0; j < readRequestArray.size(); j++) {
            std::cout << readRequestArray[j];
        }
        std::cout << std::endl;

        unsigned char* bytes = &(readRequestArray);
        for (int i = 0; i < 3; i++)
            std::cout << (float) bytes[i] << std::endl;
}
HCSR04类:公共ISensor{
公众:
HCSR04();
HCSR04(int触发器、int回波);
~HCSR04();
浮动距离厘米();
std::vector readRequest();
}
std::vector HCSR04::readRequest(){
浮动进位=距离厘米();
const unsigned char*bytes=重新解释强制转换(&C);
向量缓冲区(字节,字节+sizeof(float));
对于(int j=0;jstd::cout只需将其复制回float即可:

float value;
std::copy( readRequestArray.begin(), readRequestArray.end(), reinterpret_cast<const unsigned char*>( &value ) );
浮点值;
std::copy(readRequestArray.begin()、readRequestArray.end()、reinterpret_cast(&value));
通常,这是通过实现从
std::vector
解组到各种类型的函数来实现的,例如:

 template<typename T>
 T read( std::vector<byte>::iterator &it )
 {
     auto prev = it;
     it += sizeof(T);
     T value;
     std::copy( prev, it, reinterpret_cast<byte *>( &value ) );
     return value;
 }
模板
T读取(std::vector::iterator&it)
{
auto prev=它;
it+=sizeof(T);
T值;
标准::复制(上一次、下一次、重新解释(和值));
返回值;
}
然后使用:

 std::vector<byte> data;
 auto it = data.begin();
 auto floatValue = read<float>( it );
 auto intValue   = read<int>( it );
std::矢量数据;
auto it=data.begin();
自动浮动值=读取(it);
自动输入值=读取(it);

但您需要小心,并且仅将其用于POD类型(可能添加
std::enable_if
以强制执行)。此外,您还需要确保vector具有足够的数据,或者向函数传递第二个迭代器以验证是否有足够的数据。

只需将其复制回float:

float value;
std::copy( readRequestArray.begin(), readRequestArray.end(), reinterpret_cast<const unsigned char*>( &value ) );
浮点值;
std::copy(readRequestArray.begin()、readRequestArray.end()、reinterpret_cast(&value));
通常,这是通过实现从
std::vector
解组到各种类型的函数来实现的,例如:

 template<typename T>
 T read( std::vector<byte>::iterator &it )
 {
     auto prev = it;
     it += sizeof(T);
     T value;
     std::copy( prev, it, reinterpret_cast<byte *>( &value ) );
     return value;
 }
模板
T读取(std::vector::iterator&it)
{
auto prev=它;
it+=sizeof(T);
T值;
标准::复制(上一次、下一次、重新解释(和值));
返回值;
}
然后使用:

 std::vector<byte> data;
 auto it = data.begin();
 auto floatValue = read<float>( it );
 auto intValue   = read<int>( it );
std::矢量数据;
auto it=data.begin();
自动浮动值=读取(it);
自动输入值=读取(it);

但是您需要小心,并且只在POD类型中使用它(可能添加
std::enable_if
来强制执行它)。您还需要确保vector有足够的数据,或向函数传递第二个迭代器以验证是否有足够的数据。

要将
浮点值转换为
标准::vector
,可以使用以下命令

auto to_vector(float f)
{
    // get vector of the right size
    std::vector<unsigned char> data(sizeof(f));
    // copy the bytes
    std::memcpy(data.data(), &f, sizeof(f));
    return data;
}

auto from_vector(const std::vector<unsigned char>& data)
{
    float f;
    // make sure the vector is the right size
    if (data.size() != sizeof(f))
        throw std::runtime_error{"Size of data in vector and float do not match"};
    // copy the bytes into the float
    std::memcpy(&f, data.data(), sizeof(f));
    return f;
}

int main()
{
    float foo = 3.14;
    auto data = to_vector(foo);
    auto ret = from_vector(data);
    std::cout << ret;
}
auto to_向量(浮点f)
{
//获取正确大小的向量
向量数据(sizeof(f));
//复制字节
std::memcpy(data.data(),&f,sizeof(f));
返回数据;
}
自动从_向量(常量标准::向量和数据)
{
浮动f;
//确保向量大小正确
if(data.size()!=sizeof(f))
抛出std::runtime_error{“vector和float中的数据大小不匹配”};
//将字节复制到浮点中
std::memcpy(&f,data.data(),sizeof(f));
返回f;
}
int main()
{
浮点数foo=3.14;
自动数据=到_向量(foo);
auto ret=从_矢量(数据);

std::cout要将
float
转换为
std::vector
,可以使用以下命令

auto to_vector(float f)
{
    // get vector of the right size
    std::vector<unsigned char> data(sizeof(f));
    // copy the bytes
    std::memcpy(data.data(), &f, sizeof(f));
    return data;
}

auto from_vector(const std::vector<unsigned char>& data)
{
    float f;
    // make sure the vector is the right size
    if (data.size() != sizeof(f))
        throw std::runtime_error{"Size of data in vector and float do not match"};
    // copy the bytes into the float
    std::memcpy(&f, data.data(), sizeof(f));
    return f;
}

int main()
{
    float foo = 3.14;
    auto data = to_vector(foo);
    auto ret = from_vector(data);
    std::cout << ret;
}
auto to_向量(浮点f)
{
//获取正确大小的向量
向量数据(sizeof(f));
//复制字节
std::memcpy(data.data(),&f,sizeof(f));
返回数据;
}
自动从_向量(常量标准::向量和数据)
{
浮动f;
//确保向量大小正确
if(data.size()!=sizeof(f))
抛出std::runtime_error{“vector和float中的数据大小不匹配”};
//将字节复制到浮点中
std::memcpy(&f,data.data(),sizeof(f));
返回f;
}
int main()
{
浮点数foo=3.14;
自动数据=到_向量(foo);
auto ret=从_矢量(数据);

std::cout无符号字符*字节=&(readRequestArray);
的意义是什么?如果要访问std::vector的底层数组,请使用
.data()方法注意C++是硬的,并且尝试了解你所请求的所有东西,最好是得到一个什么是“代码”>无符号char字节= &(Read RestStar);<代码>?如果你想访问一个<代码>的基础数组:STD::vector < /代码>使用<代码>方法注意,C++是很难的,并且试图学习你所要求的一切都会得到最好的服务。