使用 ntohs() 覆盖 uint16_t 数组的只读内容

发布于 2025-01-13 02:24:26 字数 1262 浏览 3 评论 0原文

我有一个数据数组,它是从网络设备以大端格式填充的。主机上的处理器是 intel 处理器,因此默认情况下为小端。

我试图将从网络设备(小端)接收到的数据数组的内容覆盖到主机(大端)的内容,但是收到一个左值错误,表明该结构是只读的。

为简单起见,假设只有一次迭代。

我的代码如下:

class FFT_Data{
    private:
    uint16_t data[16384];

    public:
        const uint16_t* Data (void) const {
            return (const uint16_t*)data;
        }

        const void* DataBE2LE(void) const {       // conversion function
            // (const uint16_t*)ntohs(*data);     // originally i thought this was right
            data = (const uint16_t*)ntohs(*data);
        }
}

然后是这个简化的 main:

int main()
{
    FFT_Data fft;
    
    // data is received from network device

    fft.DataBE2LE();    // want to convert data array from BE to LE just once

    SomeFunctionThatDoesStuffWithData(fft.Data()); // function that then processes BE data.

    return 0;
}

所以在上面,我最初的想法是,只需执行 (const uint16_t*)ntohs(*data) 的单行函数就足够了当我在指针上操作时修改数据数组,但是我认为这是不对的,因为 ntohs 返回一个 uint16_t。

然后尝试使用 data = (const uint16_t*)ntohs(*data); 覆盖 data 的内容失败,因为 data 是私有变量,因此只读:

编译错误:FFT_Data::只读结构中的 data'

I have a data array which is populated from the network device in big endian format. The processor on the host is an intel processor, therefore by default little endian.

I am trying to overwrite the contents of the data array received from the network device (little endian) to that of the host (big endian), however receive an lvalue error saying the structure is read only.

For simplicity sake, assume that there is just one iteration.

My code is as follows:

class FFT_Data{
    private:
    uint16_t data[16384];

    public:
        const uint16_t* Data (void) const {
            return (const uint16_t*)data;
        }

        const void* DataBE2LE(void) const {       // conversion function
            // (const uint16_t*)ntohs(*data);     // originally i thought this was right
            data = (const uint16_t*)ntohs(*data);
        }
}

and then this simplified main:

int main()
{
    FFT_Data fft;
    
    // data is received from network device

    fft.DataBE2LE();    // want to convert data array from BE to LE just once

    SomeFunctionThatDoesStuffWithData(fft.Data()); // function that then processes BE data.

    return 0;
}

So in the above, I originally had the idea that a single line function that just did (const uint16_t*)ntohs(*data) would be enough to modify the data array as I am operating on a pointer, however I don't think that's right as ntohs returns a uint16_t.

Then trying to overwrite the contents of data with data = (const uint16_t*)ntohs(*data); fails as data is a private variable and thus read only:

Compile error: FFT_Data::data' in read-only structure

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

浅唱々樱花落 2025-01-20 02:24:26

DataBE2LE() 在其声明末尾被标记为 const,因此其 this 指针是指向 const FFT_Data< /code> 对象,因此其数据成员是隐式 const 并且无法修改。

由于 DataBE2LE() 想要修改 data 的内容,只需从 DataBE2LE()const 限定符即可代码>.

另外,DataBE2LE() 的返回类型应该是 void,而不是 (const) void*,因为您不是 return编码>任何东西。

class FFT_Data{
    private:
        uint16_t data[16384];

    public:
        const uint16_t* Data() const {
            return data;
        }

        void DataBE2LE() {
            *data = ntohs(*data);
        }
};

DataBE2LE() is marked as const on the end of its declaration, so its this pointer is a pointer to a const FFT_Data object, and thus its data members are implicitly const and can't be modified.

Since DataBE2LE() wants to modify the content of data, simply remove the const qualifier from the end of DataBE2LE().

Also, the return type of DataBE2LE() should be void, not (const) void* since you are not returning anything.

class FFT_Data{
    private:
        uint16_t data[16384];

    public:
        const uint16_t* Data() const {
            return data;
        }

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