c# hdf5 写string_如何在C#.NET中读取HDF5可变长度字符串属性

HDF.PInvoke

库,如何从可变长度字符串中读取,

H5T_VARIABLE

,属性?

HDF5文件看起来像:

GROUP "/" {

ATTRIBUTE "foo" {

DATATYPE H5T_STRING {

STRSIZE H5T_VARIABLE;

STRPAD H5T_STR_NULLTERM;

CSET H5T_CSET_ASCII;

CTYPE H5T_C_S1;

}

DATASPACE SCALAR

DATA {

(0): "bar"

}

}

}

foo

这样地:

long fileId = H5F.open("my-file.h5", H5F.ACC_RDWR);

long attrId = H5A.open(fileId, "foo");

long typeId = H5A.get_type(attrId);

H5A.info_t attrInfo = new H5A.info_t();

var info_result = H5A.get_info(attrId, ref attrInfo);

// Note sure if this `size` is useful.

// Docs say: For variable-length string datatypes, the returned value is

// the size of the pointer to the actual string, or sizeof(char *).

// This function does not return the size of actual variable-length

// string data.

// @See: https://support.hdfgroup.org/HDF5/doc/RM/RM_H5T.html#Datatype-GetSize

int size = H5T.get_size(typeId).ToInt32();

// Docs say: `data_size` indicates the size, in the number of characters,

// of the attribute.

// @See: https://support.hdfgroup.org/HDF5/doc/RM/RM_H5A.html#Annot-GetInfo

int data_size = (int)attrInfo.data_size;

// Docs say: In a C environment, variable-length strings will always be

// NULL-terminated, so the buffer to hold such a string must be

// one byte larger than the string itself to accommodate the NULL

// terminator.

// @See: https://support.hdfgroup.org/HDF5/doc/RM/RM_H5T.html#CreateVLString

IntPtr iPtr = Marshal.AllocHGlobal(data_size + 1); // Add one to fit NULL-terminator.

int read_result = H5A.read(attrId, typeId, iPtr);

if (H5T.is_variable_str(typeId) > 0)

{

Console.WriteLine("attribute value = {0}", Marshal.PtrToStringAnsi(iPtr));

}

attribute value = @Ã>â

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值