我写了另一个问题( https://stackoverflow.com/questions/26812721/hdf5-c-sharp-examples-to-solve-3-specific-questions-i-demonstrate-what-has-been ),该问题被标记为过于笼统,因此我正在重写以使其更清晰、更简洁且尽可能具体。我使用 Visual C++ 和 HDF5(参见:www.hdfgroup.org)来输出一些数据集。一切运作良好。为了格式化 C++ 中的两个属性,我使用
DataSpace attr_dataspace = DataSpace(H5S_SCALAR);
Attribute attribute_cardNum = dataSet.createAttribute(
attrCardNumber, PredType::STD_I32BE, attr_dataspace, PropList::DEFAULT);
attribute_cardNum.write(PredType::NATIVE_INT, &cardNumber); // write out the card number
和
StrType strdatatype(PredType::C_S1, 256); // of length 256 characters
// Create attribute and write to it
Attribute attribute_boardName = dataSet.createAttribute(
attrBoardName, strdatatype, attr_dataspace);
attribute_boardName.write(strdatatype, asciiBoardName);
例如,使用 HDF5 Java 查看器,我得到
Name Value Type Array Size
Board Name UltraMaster String,length=256 Scalar
Card number 0 32-bit integer Scalar
当我使用 C# 导出 HDF5(使用 hdf5.net 上的库)时,我得到:
Name Value Type Array Size
Board Name ˜„ String, length=256 1
Card Number 0 32-bit integer 1
请注意,数组大小现在为“1”而不是标量,并且 Board Name 的值已完全确定。 我的 C# 代码是不同的(显然:))。我有:
// Create card Number attribute
H5AttributeId attrCardId = H5A.create(dataSetId, "Card Number", typeId,
H5S.create_simple(1, new long[1] { 1 }));
H5A.write(attrCardId, new H5DataTypeId(H5T.H5Type.NATIVE_INT),
new H5Array<int> (new int[]{cardNumber}));
// Create Board Name attribute
byte[] asciiStr = ASCIIEncoding.ASCII.GetBytes("Board Name");
H5AttributeId attrBoardNameId = H5A.create(dataSetId, "Board Name",
H5T.create(H5T.CreateClass.STRING, 256), H5S.create_simple(1, new long[1] { 1 }));
H5A.write(attrBoardNameId, H5T.create(H5T.CreateClass.STRING,256),
new H5Array<string>(new string[] { GetBoardNameFromCardNum(cardNumber) }));
正如我所说,结果是不同的。最好让 C# 模仿 C++ 输出(尽管我想我可以更改 C++ 代码)。具体来说,
- 如何将数组大小设置为“标量”
- 如何输出板子名称?我认为这与我处理 C# 字符串 (unicode) 而不是 C++ ascii 字符串有关。
最佳答案
如果有人以后需要这个,对我有用的相关代码是:
// Card Number Attribute
H5AttributeId attrCardId = H5A.create(dataSetId, "Card Number", typeId, H5S.create(H5S.H5SClass.SCALAR));
H5A.write(attrCardId, new H5DataTypeId(H5T.H5Type.NATIVE_INT), new H5Array<int> (new int[]{cardNumber}));
注意 H5A.create 而不是 H5A.create_simple
// Create Board Name attribute
byte[] asciiStr = ASCIIEncoding.ASCII.GetBytes(GetBoardNameFromCardNum(cardNumber));
H5AttributeId attrBoardNameId = H5A.create(dataSetId, "Board Name", H5T.create(H5T.CreateClass.STRING, 256), H5S.create(H5S.H5SClass.SCALAR));
H5A.write(attrBoardNameId, H5T.create(H5T.CreateClass.STRING,256), new H5Array<byte>(asciiStr));
此外,我想指出以下位置有一些 HDF5.net 示例: HDF5.net。查找•HDF5DotNet 源代码和示例 获得该信息后,请查看“tests”文件夹和“example”文件夹。
干杯,
戴夫
关于c# - 需要确保 C# HDF5 输出(属性)与 Visual C++ 相同。或者 C# 中与 C++ 等效的 HDF5 格式是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26820520/