HDF5在磁盘上将32位无符号int写入64位,并读取32位无信号int
HDF5 write out 32 bit unsigned int to 64 bits on disk and read into 32 bit unsigned int
我正试图使用HDF5在磁盘上写出一个32位unsigned int
到64位unsigned long
的矢量;然后将其读回存储器中的32位CCD_ 3中。为此,我的读写函数如下所示(我为32位unsigned int
容器my_container
实现了push_back
、resize
等所有定义良好的函数):
bool write(const my_container<unsigned int>& p, const std::string& datapath, const H5::DateType& datatype):
try {
const hsize_t h5size[1] = { p.size() };
const H5::DataSpace h5space(1, h5size);
const H5::DataSet h5set = fileptr_->createDataSet(datapath, datatype, h5space);
//-Tried using void* here for data, no difference
const unsigned int* data = &(*p.begin());
h5set.write(data, datatype);
}
catch( H5::Exception& ) {
//-Handle exception here
return false;
}
return true;
}
read(my_container<unsigned int>& p, const H5::DataType& datatype, const std::string& datapath) {
H5::DataType h5set_datatype = h5set.getDataType();
const std::size_t size = (std::size_t)h5space.getSimpleExtentNpoints();
try {
if(h5set_datatype == H5::PredType::NATIVE_UINT64 && datatype == H5::PredType::NATIVE_UINT32 ) {
typedef unsigned long long u64;
typedef std::vector<u64> u64vec;
u64vec ivector;
ivector.resize(size);
void* data = (void*)(&(*ivector.begin()));
h5set.read(data, h5set_datatype);
p.resize(0);
BOOST_FOREACH(const u64 &v, ivector) {
//-I've handled the cast below using numeric cast separately
p.push_back(v);
}
} //-End compare datatypes on disk and memory
} //-End try
catch(const H5::Exception &e) {
//-Handle exception
return false;
}
return true;
}
我用实参调用write
:对my_container
、H5::Pred::NATIVE_UINT64
和read
的const引用,用实参:对my_container
和H5::Pred::NATIVE_UINT32
的引用。这可能是问题的根源之一。如果需要进一步澄清,请告诉我。基本上,当我读回来的时候,我收到了垃圾。感谢任何HDF5专家的建议。谢谢你抽出时间。
解决方案在于更改写入函数以接受文件和内存数据类型:
bool write(const my_container<unsigned int>& p, const H5::DataType& file_datatype, const H5::DataType& mem_datatype, const std::string& datapath) const {
try {
const hsize_t h5size[1] = { p.size() };
const H5::DataSpace h5space(1, h5size);
const H5::DataSet h5set = fileptr_->createDataSet(datapath, file_datatype, h5space);
const void* data = &(*p.begin());
h5set.write(data, mem_datatype);
}
catch( H5::Exception& ) {
// Handle exception
return false;
}
return true;
}
然后其余部分按预期工作-读取功能基本上没有变化;即使对于包含无符号整数对的容器也能做到这一点。
相关文章:
- 将应用程序从32位移植到64位时出现问题
- 正在解码MSVC 32位版本的程序集(作业).没有手术做什么
- 为什么我不能使用在 Visual C++ 32 位中实现运算符无符号 int() 作为数组索引的类?
- 无符号 int 与无符号长 两者都是 32 位,但我无法在不转换的情况下混合它们......为什么?
- C++将 int 分为 4 部分(32 位计算机)
- 我们是否需要担心 32 位或 64 位整数,或者我们只使用 'int'
- 在 C 语言中将 32 位 int 块连接在 16 个字符数组中
- 出于某种原因,一个int在我的ram上的重量超过了32位
- C/C++:在32位机器上的sizeof(short)、sizeof(int)、sizeof(long)、size of
- 为什么 WinAPI 对 BOOL 类型使用 int(32 位)
- “int”假定在OpenCV中始终为32位
- 将 64 位 int 作为输出传递到 32 位内联 asm
- 如何创建一个32位int和四个8位char类型的并集,每个类型都引用32位int的差分片
- 在32位或64位平台上,sizeof(int)=1
- 为什么int在64位编译器上通常是32位
- HDF5在磁盘上将32位无符号int写入64位,并读取32位无信号int
- 64位int算法在32位x86(来自c)
- 如何将unsigned long int(32位)分割成8个小块
- C++如何将int输出为32位二进制
- libjpeg/libjeg-turbo RGBA/32位int解压缩