使用 ntohs() 覆盖 uint16_t 数组的只读内容
我有一个数据数组,它是从网络设备以大端格式填充的。主机上的处理器是 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DataBE2LE()
在其声明末尾被标记为const
,因此其this
指针是指向const FFT_Data< /code> 对象,因此其数据成员是隐式
const
并且无法修改。由于
DataBE2LE()
想要修改data
的内容,只需从DataBE2LE()const
限定符即可代码>.另外,
DataBE2LE()
的返回类型应该是void
,而不是(const) void*
,因为您不是return
编码>任何东西。DataBE2LE()
is marked asconst
on the end of its declaration, so itsthis
pointer is a pointer to aconst FFT_Data
object, and thus its data members are implicitlyconst
and can't be modified.Since
DataBE2LE()
wants to modify the content ofdata
, simply remove theconst
qualifier from the end ofDataBE2LE()
.Also, the return type of
DataBE2LE()
should bevoid
, not(const) void*
since you are notreturn
ing anything.